3

I'm doing eforms framework design, In this there will be multiple products available and each product will have different kind of forms. On drag and drop it'll appear in the form panel, these form panel data's are retreived from Json file. We have json file for each forms. If i want to add validations to those fields in forms means how can I accomplish this, bcoz form fields are available in json which will be dynamically generate on drag and drop.

Can u guys help me with this.

thanks and regards rajNaveen

4

1 回答 1

2

您是否将模型与表格相关联?如果是这样,您可以将验证逻辑放入模型中。例如:

Ext.define('MyModel', {
    extend: 'Ext.data.Model',
    fields: ['field1', 'field2'],
    validations: [
         { type: 'presence', field: 'field1' }
    ]
});

有关验证配置的更多信息: http ://docs.sencha.com/ext-js/4-1/#!/api/Ext.data.validations

但是你需要一点魔法才能让他们使用表单。这是来自控制器的代码(使用 MVC):

onFormSave(): function() {
    var form = this.form.getForm(),
        updatedRecord = MyModel.create();
    form.updateRecord(updatedRecord); //saved all the data from the form, to empty object
    var errors = updatedRecord.validate(); //validate the object
    if (errors.isValid()) { //if the object is valid, then save the data to the model associated with the form.
        form.updateRecord(form.getRecord());
    }
    else {
         form.markInvalid(errors);
    }
}

这里面的逻辑非常简单,我创建了一个对象的新实例并验证它。如果验证成功,则将数据保存到表单内的对象中,该对象将被提交,如果没有,则显示错误。

于 2012-05-18T09:22:29.257 回答