2013-03-17 60 views
1

我是JSF的新手,并且需要通过一个JSF应用程序。设置字段在jsf验证后为空

我想验证一个空白字符串的密码字段。

我知道,而不是在Javascript中做它,我打电话验证功能。

我的需要是,如果密码字段的值是空的,那么它将从验证器函数验证到现在它正确地运行,但是在验证之后,当它到达时= UI bav = ck那时密码字段应该是空的。

如果密码字段为空(仅包含空格),那么我希望它将其设置为空白或保持原样。

我尝试到现在为止, JSF视图页面singin.xhtml

    <h:outputText styleClass="outputBox" style="margin: 3px;" 
         value="Password" /> 
        <h:inputSecret id="password" required="true" 
         requiredMessage="Please enter your password" 
         value="#{userActionManager.dto.password}" 
         validator="#{userActionManager.validateSamePassword}"> 

          <a4j:ajax event="change" execute="@this" bypassUpdates="true" /> 
        </h:inputSecret> 

验证方法。

public void validateSamePassword(FacesContext fc, UIComponent component, Object obj) { 
     System.out.println("UserActionManager.validateSamePassword()"); 

     String confirmPassword = (String)obj; 
     System.out.println("Password is :" + confirmPassword); 
      if(confirmPassword!=null && confirmPassword.trim().equals("")) { 
       FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_ERROR, "Password cannot be blank", " detail Passwords do not match!"); 

//   System.out.println(component.getFamily()); 
//   String field1Id = (String) component.getAttributes().get("password"); 

       // Find the actual JSF component for the client ID. 
//   UIInput textInput = (UIInput) fc.getViewRoot().findComponent("password"); 
//   textInput.setValue(""); 

       dto.setPassword(null); 

       throw new ValidatorException(message); 
      }else{ 
       dto.setPassword(confirmPassword); 
      } 
    } 

我试过选项dto.setPassword(null);但是当它返回查看时,密码字段中的空格仍然存在,我必须手动删除。

我在想什么?

回答

1

在您的验证,调用resetValue()作为参数传递的UIComponent实例

public void validateSamePassword(FacesContext fc, UIComponent component, Object obj) { 
    System.out.println("UserActionManager.validateSamePassword()"); 

    String confirmPassword = (String)obj; 
    System.out.println("Password is :" + confirmPassword); 
     if(confirmPassword!=null && confirmPassword.trim().equals("")) { 
      FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_ERROR, "Password cannot be blank", " detail Passwords do not match!"); 

      component.resetValue(); 

      throw new ValidatorException(message); 
     }else{ 
      dto.setPassword(confirmPassword); 
     } 
} 

dto.setPassword(null)不会影响在密码框中的值作为输入部件的设计都保留导致他们无法验证值。 resetValue()是一种方法,将导致组件将其值重置为未初始化状态

编辑:上述解决方案提前重置输入值。从技术上讲,验证不会失败,直到抛出一个ValidationException。为了有效地复位场:

  1. 定义一个侦听器事件将重置组件的价值只有在验证失败

    public void resetPwd(ComponentSystemEvent evt) throws IOException { 
    HtmlInputSecret txt = (HtmlInputSecret) evt.getComponent(); 
    if(!txt.isValid()){ //make sure that the validation failed 
        txt.resetValue(); 
        } 
    
    } 
    
  2. 装上监听事件的postValidate事件侦听器的密码组件。这将确保仅在验证失败后才重置该值。

    <h:inputSecret id="password" required="true" 
           requiredMessage="Please enter your password" 
           value="#{userActionManager.dto.password}" 
           validator="#{userActionManager.validateSamePassword}"> 
            <f:event name="postValidate" listener="#{userActionManager.resetPwd}"/> 
            <a4j:ajax event="change" execute="@this" bypassUpdates="true" /> 
          </h:inputSecret> 
    

这里的关键是将值在正确的时间重置。其他时间选项包括preRenderView事件和postAddToView事件。这是在所有的时间

+0

component.resetValue

dto.setPassword(null); 

(); UIComponent上没有这种方法。我尝试像((UIInput)组件).resetValue(),我发现在UIInput中有resetValue。仍然没有成功。空白值仍然存在于Field中,我想说清楚。 – Jayesh 2013-03-17 07:54:08

+0

@JayeshPatel,其实这是有道理的。我的错。从技术上讲,只有抛出'ValidationException'时,验证才会失败。我提出的当前解决方案是过早地重置值。查看我的更新以获取更适合的时机。 – kolossus 2013-03-17 08:34:17

+0

'resetValue()'未在'UIComponent'中定义。它在'EditableValueHolder'接口中定义,它由其他'UIInput'实现。 – BalusC 2013-03-17 15:07:41

1

如果一个组件被标记为无效,那么它将不会重新显示模型值(在你的情况下,从dto的值)。相反,它将重新显示其submitted value。您只需将提交的值设置为空字符串即可。

替换由

((UIInput) component).setSubmittedValue("");