如何使用extjs4在formPanel字段旁边显示服务器端验证错误?

时间:2012-02-28 01:51:02

标签: extjs4 extjs-mvc server-side-validation

我正在尝试使用Extjs4将我的类字段与我的视图字段绑定。我已经完成了我可以提交表单并获得结果的程度但不确定如果验证失败,如何在各个字段旁边显示任何错误消息。我想将这些字段直接绑定到我的类(就像我们为spring mvc做的那样 - bindingResults.rejectValue(“currentPassword”,“currentPassword.incorrect”,“密码不正确”);并且它会显示在视图中)。

我的formPanel:

{   
    xtype : 'formpanel',
    url: 'changePassword', 
    id : 'changePasswordForm', 
    method: 'POST',
    success: function(){}, 
    items : [{ 
        xtype : 'fieldset',
        id : 'passwordChange',
        title : 'Change Password',
        iconCls : 'user',
        items : [{
                xtype : 'passwordfield',
                label : 'Current Password',
                name : 'currentPassword',
                id : 'currentPassword',             
            }, {
                xtype : 'passwordfield',
                label : 'New Password'
                name : 'newPassword1',
                id : 'newPassword1',
            }, {
                xtype : 'passwordfield',
                label : 'Repeat Password',
                name : 'newPassword2',
                id : 'newPassword2',
            },]
        } ]
    },{
        xtype : 'button',
        id: 'changePassword',
    }

按钮单击时执行提交事件的控制器 -

this.getChangePasswordForm().submit();

我的班级是

@RequestMapping(value = "/changePassword", method = RequestMethod.POST)
public @ResponseBody String changePasswordInSettings(ChangePasswordForm changePasswordForm) {
    User currentUser = User.find();     

    String enteredPSHash = HashUtil
    .generateHash(changePasswordForm.getCurrentPassword(),
    currentUser.getEmail());    

    String existingPSHash = currentUser.getPassword();  

    if(!enteredPSHash.equals(existingPSHash)) {
        //bindingResults.rejectValue("currentPassword",
        //"currentPassword.incorrect", "Password is incorrect"); 
    } 
    if (!changePasswordForm.getNewPassword1().equals(changePasswordForm.getNewPassword2())) { 
        //bindingResults.rejectValue("newPassword2",
        //  "newPassword2.dontMatch", "Passwords dont match");
    }
    //if (!bindingResults.hasErrors()) { 
        String newPSHash = HashUtil.generateHash(changePasswordForm.getNewPassword1(),currentUser.getEmail()); 
        currentUser.setPassword(newPSHash); 
        //model.addAttribute("successful", new Boolean(true));      //}
    return "success";
}

如何在我的视图中显示错误消息?

2 个答案:

答案 0 :(得分:0)

感谢您提出这个问题。我不知道这个功能,但可以使用errorReader配置Form组件(参见docs) 这个功能有一个工作示例可以帮助您进行设置: http://dev.sencha.com/deploy/ext-4.0.0/examples/form/xml-form.html

答案 1 :(得分:0)

我有Ext.form.Panel:

id: 'any-id',
conroller: 'controller-id',
items: [
        {
            fieldLabel: 'Field1',
            name: 'field1',
        },
        {
            fieldLabel: 'Field2',
            name: 'field2',
        }
    ],

    buttons: [
        {
            text:'Click Me',
            itemId: 'buttonId',
            handler: 'onButtonClick'
        }
    ]

在controller-id中:

onButtonClick: function(button) {
    var window = Ext.getCmp('any-id');    
    Ext.Ajax.request({
                url: 'url',
                method : "POST",
                headers: {
                    'Content-Type': 'application/json'
                },
                jsonData: window.getForm().getValues(),
                useDefaultXhrHeader : false,
                withCredentials: false,
                success : function(form, action) {
                   var response = Ext.JSON.decode(form.responseText);
                   var puke = response.status.puke;

                   for (var key in puke) {
                       form.findField(key).markInvalid(puke[key]);
                   }
                }
    }
}

来自服务器的json:

{
  "status" : {
    "code" : 100,
    "message" : "Validation error."
    "puke" : [{"field1:", "Error message."}, {"field2:", "Another error message."}]
  }
}