如何将表单中的值与模型中的验证进行比较

时间:2013-03-26 01:57:14

标签: extjs sencha-touch sencha-touch-2

我已经设置了一个带有验证规则的模型,并希望将它们与放入表单中的信息进行比较。截至目前,我已声明了值,并验证了

Ext.define('FirstApp.view.ReviewsContainer', {
    extend: 'Ext.NavigationView',
    xtype: 'reviewscontainer',


    config: {

        title: 'Reviews',
        iconCls: 'compose',
        items: [{
                xtype: 'reviews'

            }, {
                xtype: 'button',
                align: 'right',
                ui: 'confirm',
                text: 'Leave Review',
                docked: 'bottom',
                centered: 'true',

                handler: function () {
                    if (!this.overlay) {
                        this.overlay = Ext.Viewport.add({
                            xtype: 'formpanel',
                            requires: ['FirstApp.model.ReviewCheck'],
                            id: 'reviewed',
                            modal: true,
                            hideOnMaskTap: true,
                            showAnimation: {
                                type: 'popIn',
                                duration: 250,
                                easing: 'ease-out'
                            },
                            hideAnimation: {
                                type: 'popOut',
                                duration: 250,
                                easing: 'ease-out'
                            },
                            centered: true,
                            width: Ext.os.deviceType == 'Phone' ? 260 : 400,
                            height: Ext.os.deviceType == 'Phone' ? 220 : 400,
                            styleHtmlContent: true,

                            items: [{
                                    docked: 'top',
                                    xtype: 'toolbar',
                                    title: 'Leave Review'
                                },

                                {
                                    xtype: 'fieldset',

                                    title: 'Reviews',
                                    items: [{
                                            xtype: 'textfield',
                                            name: 'name',
                                            label: 'Name',
                                            placeHolder: 'name',
                                            id: 'text'
                                        }, {
                                            xtype: 'textfield',
                                            name: 'business',
                                            label: 'Place'
                                        }, {
                                            xtype: 'textfield',
                                            name: 'rating',
                                            label: 'Rating'
                                        }, {
                                            xtype: 'textareafield',
                                            name: 'review',
                                            label: 'Review'
                                        }
                                    ]
                                }, {
                                    items: [{
                                            xtype: 'button',
                                            text: 'Submit',
                                            ui: 'confirm',
                                            handler: function () {

                                                console.log("Hello");
                                                //var values = Ext.getCmp('reviewed').getValues();

                                                // var text= Ext.getCmp('text').getValue();

                                                //   var value = Ext.ComponentQuery.query('#text')[0].getValue();


                                                //var reviewCheckForm = this.getReviewCheck();
                                                //var currentForm = reviewCheckForm.getRecord();
                                                // var newValues = reviewed.getValues();

                                                // currentForm.set("name", newValues.name);
                                                // currentForm.set("business", newValues.business);
                                                // currentForm.set("review", newValues.review);


                                                var form = formPanel.getForm(); //Get the basicform instance
                                                var record = form.getRecord(); //Get your record loaded in the form
                                                form.updateRecord(record); //Update your record with the current form values
                                                var errors = record.validate(); //Validate your record



                                                // console.log(newValues);
                                                // var data = Ext.create('FirstApp.model.ReviewCheck', {
                                                //         name: 'dfhd',
                                                //     business: 'sdfs',
                                                //     id: 1,
                                                //   review: 'sfsd'
                                                //  });

                                                //   var errors = data.validate();


                                                // var errors = currentForm.validate();
                                                console.log('Number of errors: ' + errors.getCount());




                                                if (!errors.isValid()) {
                                                    console.log('Number of errors: ' + errors.getCount());

                                                    errors.each(function (item, index, length) {
                                                        // Each item in the errors collection is an instance of the Ext.data.Error   class.
                                                        console.log('Field "' + item.getField() + '" ' + item.getMessage());
                                                    });
                                                    Ext.Msg.alert('Form is invalid!');
                                                } else {

                                                    var values = Ext.getCmp('reviewed').getValues();
                                                    // prints the values filled in the form 
                                                    // text fields of name, email and message.     
                                                    console.log(values.name + "," + values.place + "," + values.rating);


                                                    Ext.Ajax.request({
                                                        url: 'http://insert.php',
                                                        params: values,
                                                        success: function (response) {
                                                            var text = response.responseText;
                                                            Ext.getCmp('reviewed').reset();
                                                            Ext.Msg.alert('Success', "Review successfully created.", Ext.getCmp('reviewed').hide(), Ext.StoreMgr.get('Reviews').load());
                                                            console.log("Im Here");
                                                        }
                                                    });
                                                }
                                            }
                                        }
                                    ]
                                }
                            ],
                            scrollable: false
                        });
                    }

                    this.overlay.show();
                }
            }

        ]
    }
})



Ext.define('FirstApp.model.ReviewCheck', {
    extend: 'Ext.data.Model',
    config: {
        fields: [{
                name: 'name',
                type: 'string'
            }, {
                name: 'business',
                type: 'string'
            }, {
                name: 'id',
                type: 'int'
            }, {
                name: 'review',
                type: 'string'
            }
        ], // fields
        validations: [{
                field: 'name',
                type: 'presence',
                error: 'Name must be present',
                message: 'Name is required.'
            }, {
                field: 'business',
                type: 'presence',
                message: 'Place is required.'
            }, {
                field: 'review',
                type: 'presence',
                message: 'Review is required.'

            }
        ] // validations
    } // config
}); // define()

如何设置名称,业务,ID和评论从表单

中获取的值

3 个答案:

答案 0 :(得分:0)

首先获取您的视图实例

var reviewCheckForm = this.getReviewCheck();

然后从中获取记录

var currentForm = reviewCheckForm.getRecord();

然后从同一表格中获取新值

var newValues = reviewCheckForm.getValues();

然后将新值设置为记录并验证它

currentForm.set("name", newValues.name);
currentForm.set("business", newValues.business);
currentForm.set("review", newValues.review);

然后验证它

var errors = currentForm.validate();

if (!errors.isValid()) {
    Ext.Msg.alert('Wait!', errors.getByField("title")[0].getMessage(), Ext.emptyFn);
    currentForm.reject();
    return;
}

您可以按照this简单教程获取更多信息,以供参考,here是模型验证部分。

答案 1 :(得分:0)

您可以使用updateRecordform功能。 不要手动设置@Mayur在他的示例中显示的所有属性。

这样的事情:

{
    xtype: 'button',
    text: 'Submit',
    ui: 'confirm',
    handler: function () {
        var formPanel = this.up('formpanel'); //Get the formpanel
        var form = formPanel.getForm(); //Get the basicform instance
        var record = form.getRecord(); //Get your record loaded in the form
        form.updateRecord(record); //Update your record with the current form values
        var errors = record.validate(); //Validate your record

        console.log('Number of errors: ' + errors.getCount());

        if (!errors.isValid()) {
            console.log('Number of errors: ' + errors.getCount());

            errors.each(function (item, index, length) {
                // Each item in the errors collection is an instance of the Ext.data.Error   class.
                console.log('Field "' + item.getField() + '" ' + item.getMessage());
            });
            Ext.Msg.alert('Form is invalid!');
        } 
        //...
    }
}

答案 2 :(得分:0)

我有类似的问题...... JSF + Richfaces 基本上,我有多个标签......类似......(不是确切的代码)

<rich:tab name="a">
  <inputText id="a1" values="">
  <inputText id="a2" values="">
</rich:tab>

<rich:tab name="b">
  <inputText id="b1" values="">
  <inputText id="b2" values="">
</rich:tab>

步骤 1)用户首先转到标签“a”并将“a1”值更改为“newValue” 2)不提交表格..转到标签“b”。 由于用户没有提交表格,用户应该收到警告信息说“你在标签中有未保存的数据”....

如何实现这个?

谢谢..真的很感激。