2010-11-12 71 views
1

我使用seam 2.2.0.GA. 我的密碼/確認字段有redisplay =「false」,所以當前密碼不在html中。 validateEquality在確認字段中。h:inputSecret redisplay =「false」required =「false」><s:validateEquality:確認字段要麼不需要檢查

我想要以下行爲:當我創建一個新的實體時,應該都需要密碼/確認字段。當我編輯一個實體時,不應該要求這兩個字段,但在任何情況下都應該檢查相等性。爲此我有h:inputSecret required =「#{required}」並計算param「required」。

有是兩個問題:

1)S:validateEquality有「需要」太屬性,但它看起來馬車:如果我硬編碼真的還是假的,它工作正常,但如果我使用所需= 「#{required}」並且在對話期間這個參數改變了,驗證器仍然表現得好像param沒有改變。

2)s:validateEquality required =「false」如果confirm字段爲空,則不檢查相等性。

#{sandboxController.now} 

<ui:param name="label" value="password"/> 
<ui:param name="labelConfirm" value="confirm password"/> 
<ui:param name="message" value="not the same"/> 
<ui:param name="id" value="bbb"/> 

<ui:param name="redisplay" value="#{sandboxBean.redisplay}"/> 
<ui:param name="required" value="#{sandboxBean.required}"/> 
<ui:param name="value" value="#{sandboxBean.password}" /> 

    <a4j:form id="personalForm"> 

<a4j:region> 
required: <h:selectBooleanCheckbox value="#{required}"> 
    <a4j:support event="onclick" reRender="passwordpanel" /> 
</h:selectBooleanCheckbox><br/> 
redisplay: <h:selectBooleanCheckbox value="#{redisplay}"> 
    <a4j:support event="onclick" reRender="passwordpanel" /> 
</h:selectBooleanCheckbox><br/> 
</a4j:region> 

<a4j:outputPanel id="passwordpanel"> 
password: #{value}<br/> 
    <s:decorate styleClass="fieldForm #{formClass}"> 
     <div class="fieldLabel #{labelClass}"> 
      <s:label styleClass="#{invalid?'error':''}" > 
       #{label} 
       <s:span styleClass="required" rendered="#{required}">*</s:span> 
      </s:label> 
     </div> 

     <div class="fieldInput"> 
      <s:validateAll> 
       <h:inputSecret id="#{id}" value="#{value}" required="#{required}" redisplay="#{redisplay}"> 
       </h:inputSecret> 
      </s:validateAll> 
      <s:message styleClass="error"/> 
     </div> 
    </s:decorate> 
    <s:decorate styleClass="fieldForm #{formClass}"> 
     <div class="fieldLabel #{labelClass}"> 
      <s:label styleClass="#{invalid?'error':''}" > 
       #{labelConfirm} 
       <s:span styleClass="required" rendered="#{required}">*</s:span> 
      </s:label> 
     </div> 
     <div class="fieldInput"> 
      <s:validateAll> 
       <h:inputSecret value="#{value}" required="#{required}" redisplay="#{redisplay}"> 
        <s:validateEquality for="#{id}" required="#{required}" message="#{message}"/> 
       </h:inputSecret> 
      </s:validateAll> 
      <s:message styleClass="error"/> 
     </div> 
    </s:decorate> 
</a4j:outputPanel> 

<a4j:commandLink value="submit" action="#{sandboxController.getNow}" reRender="personalForm" /><br/> 


    </a4j:form> 

SandboxBean.java:

package org.foo.model; 

import java.io.Serializable; 

public class SandboxBean implements Serializable { 
    private static final long serialVersionUID = 1L; 
    private String password = "abcde"; 
    private boolean required=false; 
    private boolean redisplay=false; 

    public void setRequired(boolean required)  {   this.required = required;  } 
    public boolean isRequired()  {   return required;  } 
    public String getPassword() {   return password;  } 
    public void setPassword(String password) {   this.password = password;  } 
    public void setRedisplay(boolean redisplay)  {   this.redisplay = redisplay;  } 
    public boolean isRedisplay()  {   return redisplay;  } 
} 

SandboxController.java:

package org.foo.logic.model; 

import java.io.Serializable; 
import java.util.Date; 

import org.foo.model.SandboxBean; 
import org.jboss.seam.ScopeType; 
import org.jboss.seam.annotations.Begin; 
import org.jboss.seam.annotations.Factory; 
import org.jboss.seam.annotations.Logger; 
import org.jboss.seam.annotations.Name; 
import org.jboss.seam.annotations.Out; 
import org.jboss.seam.annotations.Scope; 
import org.jboss.seam.log.Log; 

@Name("sandboxController") 
@Scope(ScopeType.CONVERSATION) 
public class SandboxController implements Serializable{ 
    private static final long serialVersionUID = 1L; 

    @Logger Log log; 

    @Out(required = false) 
    SandboxBean sandboxBean; 

    @Begin(join=true) 
    @Factory("sandboxBean") 
    public void initSandBoxBean(){ 
     sandboxBean = new SandboxBean(); 
    } 

    public Date getNow() { 
     return new Date(); 
    } 
} 

回答

0

問題1:

下面的代碼將您的 「要求」 現場靜,我相信:

<ui:param name="required" value="#{sandboxBean.required}"/> 

問題2:

http://seamframework.org/Community/CustomValidatorUsingTwoFields

使用標有相應的註釋自定義驗證,然後你可以自定義驗證調用。

@Validator 
public class PasswordValidator implements javax.faces.validator.Validator, Serializable{ 
... 
throw new ValidatorException(new FacesMessage("Passwords do not match.")); 
+0

爲了清楚起見,該UI PARAM接通字段靜態原因是EL參考「#{sandboxBean.required}」被轉換爲值首先加載頁面時。當底層Bean值發生變化時,該值不會被重新解釋。是有一些原因,你不只是在頁面中使用的其餘部分「#{} sandboxBean.required」在XHTML參考? – 2011-02-16 19:01:42

+0

經過這麼多時間...... 然後,我決定用這個: 所需=「真」重新顯示=「真正的」轉換器=「passwordConverter」 而使用自定義轉換器等等領域從來沒有空 – basin 2011-02-17 08:25:50

0
public class PasswordConverter 
    implements Converter 
{ 
    public static String DONT = "__dontchange__"; 

    public Object getAsObject(FacesContext context, UIComponent component, String value) 
    { 
     if (DONT.equals(value) && component instanceof UIOutput) { 
      return ((UIOutput)component).getValue(); 
     } else { 
      return value; 
     } 
    } 

    public String getAsString(FacesContext context, UIComponent component, Object value) 
    { 
     if (value instanceof String && component instanceof UIOutput && ((String)value).length() > 0) { 
      return DONT; 
     } else if (value == null) { 
       return null; 
     } else { 
      return value.toString(); 
     } 
    } 

}