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

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

问题描述

Sencha 的这篇文章介绍了如何使用内置的验证规则(存在、长度、格式、包含、排除),并提到添加自定义规则很容易,但从未解释如何执行.我在谷歌上搜索了高低并阅读了 sencha 文档,但我找不到任何关于如何做的信息.有什么想法吗?

http://www.sencha.com/学习/使用验证和关联-in-sencha-touch

解决方案

我认为这是文档中的小错误之一.我通过添加一些代码让它们工作

if (Ext.data) {Ext.data.validations.custom = 函数(配置,值){如果(配置&& Ext.isFunction(config.fn)){//这应该是模型如果(配置.self){返回 config.fn.call(config.self, value);} 别的 {返回 config.fn(value);}}别的{返回假;}};Ext.data.validations.customMessage = "错误";}

然后要向模型添加验证,请将对象添加到模型的验证数组,并将类型设置为自定义",例如

<代码>{类型:'custom',字段:'SomeField',消息:你的字段不好",fn:函数(SomeFieldValueForThisInstance){//添加一些验证代码.this 指针被设置为模型对象//所以你可以调用 this.get("SomeOtherFieldToCheck")//或任何其他实例方法//如果字段好返回真;//别的返回假;}}

更新:@salgiza 是对的,为了正确设置this"指针,我忘记提及几个步骤.如果您查看 sencha touch 代码,您会看到在 Ext.data.Model 构造函数的末尾,它会检查对象上是否定义了 init 函数,如果是,则调用它

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

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

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

然后在上面的自定义验证函数中,只需检查配置是否有 self 指针,如果有,则使用 self 调用它.我已经编辑了上面的代码以使用 self.

注意:我没有看到模型的 init 函数被记录下来,所以如果 sencha 去掉它,你必须以其他方式将 this 指针添加到模型的验证中.>

抱歉,如果这给任何人造成了混淆.

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

解决方案

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";
}

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;
    }
}

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();

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.

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天全站免登陆