簡體   English   中英

如何在Sencha Touch中向模型添加自定義驗證規則

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

Sencha的這篇文章介紹了如何使用內置的驗證規則(存在,長度,格式,包含,排除),並提到添加自定義規則很容易,但是沒有解釋如何去做。 我已經googled高低,並閱讀了sencha文檔,但我找不到任何關於如何做到這一點。 有任何想法嗎?

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

我認為這是文檔中的一個小錯誤。 我通過添加一些代碼讓他們工作

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',例如

{ 
    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是對的,我忘了提幾個步驟,以正確設置'this'指針。 如果您查看sencha觸摸代碼,您將看到在Ext.data.Model的構造函數的末尾,它會檢查是否在對象上定義了init函數,如果是,則調用它

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

定義模型后,可以向原型添加init函數。 在該函數中,迭代對象的驗證並添加對此的引用。 此步驟應在創建任何模型之前完成。

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

然后在上面的自定義驗證函數中,只檢查配置是否有自我指針,如果有,請用self調用它。 我編輯了上面的代碼來使用self。

注意:我沒有看到模型的init函數被記錄,所以如果sencha擺脫它,你將不得不以其他方式將this指針添加到模型的驗證中。

對不起,如果這給任何人造成了混亂。

我認為向模型添加復雜自定義驗證的最簡單方法是覆蓋validate方法。 如下所示,由於父調用,它支持內置驗證類型。

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

我稍微調整了Jason的代碼,用於sencha touch 2(因為驗證現在位於模型的config屬性中)。 我建議創建一個基類,所有其他模型類都將從該基類繼承。 然后,一旦你完成了,你可以使用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