简体   繁体   English

如何在Sencha Touch中向模型添加自定义验证规则

[英]How to add a custom validation rule to a model in Sencha Touch

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. Sencha的这篇文章介绍了如何使用内置的验证规则(存在,长度,格式,包含,排除),并提到添加自定义规则很容易,但是没有解释如何去做。 I've googled high and low and read the sencha docs, but I can't find anything on how to do it. 我已经googled高低,并阅读了sencha文档,但我找不到任何关于如何做到这一点。 Any Ideas? 有任何想法吗?

http://www.sencha.com/learn/using-validations-and-associations-in-sencha-touch 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', eg 然后,为模型添加验证,将对象添加到模型的验证数组中,类型设置为'custom',例如

{ 
    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. 更新: @salgiza是对的,我忘了提几个步骤,以正确设置'this'指针。 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 如果您查看sencha触摸代码,您将看到在Ext.data.Model的构造函数的末尾,它会检查是否在对象上定义了init函数,如果是,则调用它

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

After you define your model you can add an init function to the prototype. 定义模型后,可以向原型添加init函数。 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. 然后在上面的自定义验证函数中,只检查配置是否有自我指针,如果有,请用self调用它。 I've edited the code above to use self. 我编辑了上面的代码来使用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. 注意:我没有看到模型的init函数被记录,所以如果sencha摆脱它,你将不得不以其他方式将this指针添加到模型的验证中。

Sorry if this caused confusion for anybody. 对不起,如果这给任何人造成了混乱。

i think the easiest way to add complex custom validations to your model is to overwrite the validate method. 我认为向模型添加复杂自定义验证的最简单方法是覆盖validate方法。 see below, due to the parent call, it supports the builtin validation types. 如下所示,由于父调用,它支持内置验证类型。

validate: function() {
    var me = this;
    var errors = this.callParent(arguments);

    /* custom complex validations here */
    if(true !== me.get('checkOne') &&
       true !== me.get('checkTwo') &&
       true !== me.get('checkThree')) {
       errors.add(Ext.create('Ext.data.Error', {
                    field  : 'checkOne',
                    message: 'Choose at least one check, e.g. checkOne'
                }));
    }

    return errors;
}

I've slightly adapted the code from Jason for sencha touch 2 (since validations is now in the config property of the model). 我稍微调整了Jason的代码,用于sencha touch 2(因为验证现在位于模型的config属性中)。 I suggest creating a base class from which all your other model classes would inherit. 我建议创建一个基类,所有其他模型类都将从该基类继承。 Then, once you've done that, you can use jason's technics for adding custom validations in Ext.data.validations singleton. 然后,一旦你完成了,你可以使用jason的技术在Ext.data.validations singleton中添加自定义验证。

Ext.define('MyApp.model.CustomModelBase', {
    extend: 'Ext.data.Model',

    //adding an initializer to let custom validators access "self"
    init : function () {
        var i, len;
        if (this.config.validations) {
            for (i = 0, len = this.config.validations.length; i < len; i++) {
                this.config.validations[i].self = this;
            }
        }
    }
});

需要实现自定义验证,googled,也发现这个tomalex0 / SenchaTouch-Form-Validation在github上

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM