2011-11-06 38 views
1

当您键入电子邮件,旧密码,新密码并重新为您的配置文件修改新密码时,我有一些基本形式。我试图达到的目的是在通行证错误时显示反馈信息。一段代码如下所示:添加了反馈面板,但未显示所有消息

public class EditProfileForm extends Form<Void> 
{ 
private static final long serialVersionUID = 1L; 

// El-cheapo model for form 
private final ValueMap properties = new ValueMap(); 

private ModalWindow modalWindow; 

@SpringBean 
UserManager userManager; 

@SpringBean 
Validator validator; 

private User user; 

private static final String EMAIL = "mp-email"; 
private static final String OLD_PASS = "mp-oldpass"; 
private static final String NEW_PASS = "mp-newpass"; 
private static final String NEW_PASS_REPEAT = "mp-newpassrepeat"; 

private static final String ERROR_WRONG_PASS = "Wrong pass"; 
private static final String ERROR_PASS_DIFF = "Pass diff"; 
private static final String ERROR_EMAIL_INVALID = "Wrong email"; 
private static final String ERROR_EMAIL_EXISTS = "Email used"; 

public EditProfileForm(String id, final ModalWindow modalWindow,final User u) 
{ 
    super(id); 
    this.user = u; 
    this.modalWindow = modalWindow; 

    add(new TextField<String>(EMAIL, new PropertyModel<String>(properties, EMAIL)).setRequired(true).setOutputMarkupId(true)); 
    add(new PasswordTextField(OLD_PASS, new PropertyModel<String>(properties, OLD_PASS)).setOutputMarkupId(true)); 
    add(new PasswordTextField(NEW_PASS, new PropertyModel<String>(properties, NEW_PASS)).setOutputMarkupId(true)); 
    add(new PasswordTextField(NEW_PASS_REPEAT, new PropertyModel<String>(properties, NEW_PASS_REPEAT)).setOutputMarkupId(true)); 

    final FeedbackPanel feedbackPanel = new FeedbackPanel("feedback"); 
    feedbackPanel.setOutputMarkupId(true); 
    //feedbackPanel.setMaxMessages(5); 

    add(feedbackPanel); 

    AjaxSubmitLink closeBtn = new AjaxCloseCancelBtn("close-x", this); 
    AjaxSubmitLink cancelBtn = new AjaxCloseCancelBtn("cancel", this); 

    add(new AjaxSubmitLink("save", this) 
    { 
     private static final long serialVersionUID = 1L; 

     @Override 
     protected void onSubmit(AjaxRequestTarget target, Form<?> form) 
     { 
      if (!user.getCryptedPassword().equals(CypherUtil.encodeMd5(getOldPassword()))) 
      { 
       error(ERROR_WRONG_PASS); 
      } 
      else if (!getNewPassword().equals(getNewPasswordRepeat())) 
      { 
       error(ERROR_PASS_DIFF); 
      } 
      else if (!validator.validateEmailFormat(getEmail())) 
      { 
       error(ERROR_EMAIL_INVALID); 
      } 
      else if (validator.emailExists(getEmail())) 
      { 
       error(ERROR_EMAIL_EXISTS); 
      } else 
      { 
       //save user data 
      } 
     } 

     @Override 
     protected void onError(AjaxRequestTarget target, Form<?> form) 
     { 
      target.add(feedbackPanel); 
     } 
    }); 

    add(closeBtn); 
    add(cancelBtn); 
} 

/** 
* @return 
*/ 
private String getEmail() 
{ 
    return properties.getString(EMAIL); 
} 

/** 
* @return 
*/ 
private String getOldPassword() 
{ 
    return properties.getString(OLD_PASS); 
} 

/** 
* @return 
*/ 
private String getNewPassword() 
{ 
    return properties.getString(NEW_PASS); 
} 

/** 
* @return 
*/ 
private String getNewPasswordRepeat() 
{ 
    return properties.getString(NEW_PASS_REPEAT); 
} 

} 

现在,当我将这些表单留空时,我收到了很好的反馈信息,即有必要的字段。同时,当我输入错误的传球,电子邮件或者其他我什么都没有,我的服务器日志离开味精这样的:

WARNING: Component-targetted feedback message was left unrendered. 
This could be because you are missing a FeedbackPanel on the page. 
Message: [FeedbackMessage message = "Wrong pass", reporter = save, level = ERROR] 

形式是网页是一个弹出窗口内:

public class ProfilePopup extends WebPage 
{ 
private static final long serialVersionUID = 2929269801026361184L; 

public ProfilePopup(final ModalWindow modal, final User user) 
{ 
    add(new EditProfileForm("profileModifyForm", modal, user).setOutputMarkupId(true)); 
} 
} 

和HTML,将反馈的形式外也没有帮助:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml" 
    xmlns:wicket="http://wicket.apache.org/dtds.data/wicket-xhtml1.4-strict.dtd"> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> 
</head> 
<body class="stats-popup-body"> 
<div class="stats-popup" id="car-info-edit-popup"> 
    <p class="popup_title"> Edytuj Profilu </p> 
    <form wicket:id="profileModifyForm" class="stats-popup-form">   
     <div> 
      <label class="popup_field_label">Email:</label> 
      <input type="text" wicket:id="mp-email" /> 
     </div> 
     <div class="clear9"></div> 
     <div> 
      <label class="popup_field_label">Stare hasło:</label> 
      <input type="password" name="E-mail" title="E-mail1" id="E-mail2" wicket:id="mp-oldpass" /> 
     </div> 
     <div> 
      <label class="popup_field_label">Nowe hasło:</label> 
      <input type="password" wicket:id="mp-newpass" /> 
     </div> 
     <div> 
      <label class="popup_field_label">Powtórz nowe hasło:</label> 
      <input type="password" wicket:id="mp-newpassrepeat" /> 
     </div> 
     <span wicket:id="feedback"></span> 
     <div class="button-box-bottom"> 
      <input class="btn btn_save" style="margin-right: 9px;" 
       wicket:id="save" type="button" value="Zapisz" 
       onmousemove="this.className='btn btn_save btn_hover'" 
       onmouseout="this.className='btn btn_save'" /> 
      <input 
       class="btn btn_cancel" wicket:id="cancel" 
       value="Anuluj" type="button" 
       onmousemove="this.className='btn btn_cancel btn_hover'" 
       onmouseout="this.className='btn btn_cancel'" /> 
     </div> 
     <div class="stats-popup-close-x" wicket:id="close-x"></div> 
    </form> 
</div> 
</body> 
</html> 

回答

11

这是不好的做法,在onSubmit执行输入验证,因为一旦你到达那里,(无效)输入已达到楷模。正如您已经遇到的,在验证阶段结束后调用onSubmit,所以输入将被视为有效,因此不会调用Form.onError

如果您需要执行不是那么简单的验证或涉及两个或多个组件输入的验证,请改为使用FormValidator(在您的构造函数中将IFormValidator添加到Form)。请记住,用户输入未达到FormValidator.validate()中的组件型号,因此您需要使用Component.getConvertedInput()来验证输入。

如果您的验证仅涉及一个组件,并且仅对其输入进行操作,则建议您改用IValidator

此外,请注意Wicket提供的验证器可以执行您想要执行的一些验证:您可以使用EqualPasswordInputValidatorEmailAddressValidator

例如:

emailField.add(EmailAddressValidator.getInstance()); 
form.add(new EqualPasswordInputValidator(passwordField, passwordRepeatField)); 

看看这个(有点过时)wiki页面的信息,FormValidators:Validating related fields

+0

Whoaaa非常感谢队友!这解决了我所有的问题:) – kamil