简体   繁体   English

如何在sencha touch / ext js模型中编写验证

[英]how to write validation in sencha touch / ext js model

I have a model like this 我有这样的模特

Ext.define('app.model.TeamEmpAssignment', {
            extend : 'Ext.data.Model',
            config : {
                idProperty : 'teamEmpId',
                fields : [{
                        name : 'teamEmpId',
                        type : 'int'
                    }, {
                        name : 'actName'
                    }, {
                        name : 'beginDateTime'
                    }, {
                        name : 'endDateTime'
                    },

                ],

                validations : [{
                        field : 'beginDateTime',
                        type : 'presence',
                        message : 'Effective Begin Date Time required'
                    }, {
                        field : 'endDateTime',
                        type : 'presence',
                        message : 'Effective End Date Time required'
                    },
                ],

            }

        });

I have to write a validation to compare that endDateTime > startDateTime 我必须编写一个验证以比较endDateTime> startDateTime

I am trying sencha touch 2.3.1 我正在尝试sencha touch 2.3.1

I know this is an old question, but I ran into it while dealing with the same problem and wanted to share my approach. 我知道这是一个老问题,但是我在处理相同问题时遇到了这个问题,并希望分享我的方法。

I created a method in the model, which does the custom validation, something like: 我在模型中创建了一个方法,用于进行自定义验证,类似于:

Ext.define('app.model.TeamEmpAssignment', {
    extend : 'Ext.data.Model',
    config : {
        // config here
    },

    checkDates: function (errors) {
        // assuming the dates are timestamps
        if (this.get('beginDateTime') > this.get('endDateTime')) {
            errors.add(Ext.create('Ext.data.Error', {
                field  : 'beginDateTime',
                message: 'Begin date can\'t be after end date'
            }));
        }
    }

});

You will notice that checkDates has one argument - errors. 您会注意到checkDates有一个参数-错误。 This argument is actually the Ext.data.Errors object return by the validate method of the model. 此参数实际上是模型的validate方法返回的Ext.data.Errors对象。 So we can do something like: 因此,我们可以执行以下操作:

var record = Ext.create('Ext.data.Errors');
// set some values here
var errors = record.validate();
record.checkDates(errors);

console.log(errors.isValid());

We can use isValid, because all it does is check whether the Ext.data.Errors collection has items. 我们可以使用isValid,因为它所做的只是检查Ext.data.Errors集合中是否包含项。

I am currently using it like this, however it will be quite easy to override the validate method, so you don't have to call the custom validation externaly. 我目前正在这样使用它,但是重写validate方法非常容易,因此您不必外部调用自定义验证。

Ext.define('app.model.TeamEmpAssignment', {
    extend : 'Ext.data.Model',
    config : {
        // config here
    },

    checkDates: function (errors) {
        // assuming the dates are timestamps
        if (this.get('beginDateTime') > this.get('endDateTime')) {
            errors.add(Ext.create('Ext.data.Error', {
                field  : 'beginDateTime',
                message: 'Begin date can\'t be after end date'
            }));
        }
    },

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

        this.checkDates(errors);

        return errors;
    }

});

The validation types come from the Ext.data.validations singleton. 验证类型来自Ext.data.validations单例。 You can add your own validators here, however the method does not supply the all the fields in the model (which it sounds like you will need). 您可以在此处添加自己的验证器,但是该方法不能提供模型中的所有字段(听起来像您需要的)。 We would advise moving your validators to a different spot (outside the model), maybe on the related form component (after a load or before a save). 我们建议将验证器移动到模型外部的其他位置(可能在相关表单组件上)(在加载之后或保存之前)。

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

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