How to add a custom validation rule to a model in Sencha Touch

前端 未结 4 602
野趣味
野趣味 2021-01-04 13:33

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 do

4条回答
  •  囚心锁ツ
    2021-01-04 13:42

    I've slightly adapted the code from Jason for sencha touch 2 (since validations is now in the config property of the model). 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.

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

提交回复
热议问题