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



我正在尝试使用Extjs4将类字段与视图字段绑定。我已经做到了可以提交表单并返回结果的程度,但不确定如果验证失败,如何在相应字段旁边显示任何错误消息。我想将这些字段直接绑定到我的类(就像我们对springmvc所做的那样,例如bindingResults.rejectValue("currentPassword"、"currentPassword.incorrect"、"Password is correct");并且它被显示在视图中)。

我的表单面板:

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

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

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

我有带的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'
        }
    ]

控制器内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."}]
  }
}

相关内容

  • 没有找到相关文章

最新更新