如何在Sencha Touch中为模型添加自定义验证规则 [英] How to add a custom validation rule to a model in Sencha Touch

查看:122
本文介绍了如何在Sencha Touch中为模型添加自定义验证规则的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Sencha的这篇文章涵盖了如何使用内置的验证规则(存在,长度,格式,包含,排除),并提到添加自定义规则很容易,但并没有解释如何做到这一点。我已经搜索高低,阅读了sencha文档,但我找不到任何关于如何做的事情。任何想法?

This article by Sencha covers how to use the built in validation rules (presence, length, format, inclusion, exclusion) and mentions that adding custom rules is easy, but doesn't ever explain how to do it. I've googled high and low and read the sencha docs, but I can't find anything on how to do it. Any Ideas?

http://www.sencha.com/learn/using-validations-and-associations-in-sencha-touch

推荐答案

我认为这是文档中的轻微错误之一。如果(Ext.data){
Ext.data.validations,则可以通过添加一些代码

I think it's one of the slight errors in the documentation. I got them to work by adding some code

if (Ext.data) {
    Ext.data.validations.custom = function (config, value) {
        if (config && Ext.isFunction(config.fn)) {
            //this should be the model
            if (config.self) {
                return config.fn.call(config.self, value);
            } else {
                return config.fn(value);
            } 
        }
        else 
        {
            return false;
        }
    };
    Ext.data.validations.customMessage = "Error";
}

然后为模型添加验证,将一个对象添加到模型的验证数组类型设置为'custom',例如

Then to add a validation to a model, add an object to the model's validations array with type set to 'custom', e.g.

{ 
    type: 'custom', field: 'SomeField', message: "Your field is bad",
    fn: function (SomeFieldValueForThisInstance) {
       //Add some validation code.  The this pointer is set to the model object
       //so you can call this.get("SomeOtherFieldToCheck")
       //or any other instance method

       //if the field is good
       return true;
       //else
       return false;
    }
}

更新: @salgiza是对的,有几个步骤我忘了提到,以正确设置'这个'指针。如果你看到sencha触摸代码,你会看到在Ext.data.Model的构造函数的末尾,它检查是否有一个在对象上定义的init函数,如果是,调用它

Update: @salgiza was right, there's a few steps I forgot to mention in order to set the 'this' pointer correctly. If you look in the sencha touch code you'll see that at the end of Ext.data.Model's constructor it checks to see if there's an init function defined on the object, and if so, calls it

        if (typeof this.init == 'function') {
            this.init();

定义模型后,您可以向原型添加一个init函数。在该函数中,迭代对象的验证,并添加对此的引用。这个步骤应该在创建任何模型之前完成。

After you define your model you can add an init function to the prototype. In the function, iterate over the validations for the object and add a reference to this. This step should be done before any of the models are created.

    YourModel.prototype.init = function () {
        var i, len;
        if (this.validations) {
            for (i = 0, len = this.validations.length; i < len; i++) {
                this.validations[i].self = this;
            }
        }
    };

然后在上面的自定义验证功能中,只需检查配置是否具有自我指针,如果它用自我称呼。我已经编辑了上面的代码来使用自己。

Then in the custom validation function above, just check if the config has a self pointer and if it does, call it with self. I've edited the code above to use self.

注意:我没有看到模型的init函数记录在案,所以如果sencha得到除此之外,你必须以这种方式将这个指针添加到模型的验证中。

Note: I don't see the Model's init function documented, so if sencha gets rid of it, you'll have to add the this pointer to the model's validations some other way.

对不起,如果这造成任何人的混乱。

Sorry if this caused confusion for anybody.

这篇关于如何在Sencha Touch中为模型添加自定义验证规则的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆