2011-09-20 15 views
15

私はJSFで2つのパスワードフィールドを検証しようとしていますが、今まではうまくいきませんでした。Googleで検索しましたが、すべてがJSF 1.2であり、かなり混乱しています。ajaxで2つのパスワードフィールドを検証する方法は?

これは私がこれまでやっているものです:

 <h:outputLabel for="password" value="Password:" /> 
     <h:inputSecret id="password" value="#{register.user.password}" > 
      <f:ajax event="blur" listener="#{register.validatePassword}" render="m_password" /> 
     </h:inputSecret> 
     <rich:message id="m_password" for="password"/> 

     <h:outputLabel for="password_2" value="Password (again):" /> 
     <h:inputSecret id="password_2" value="#{register.user.password_2}" > 
      <f:ajax event="blur"  listener="#{register.validatePassword}" /> 
     </h:inputSecret> 

これは私それが私のコントローラである方法です:

public void validatePassword() { 
    FacesMessage message; 

    if (!user.getPassword().equals(user.getPassword_2())){ 
     message = new FacesMessage(FacesMessage.SEVERITY_ERROR, null, "different password"); 
    }else{ 
     message = new FacesMessage(FacesMessage.SEVERITY_INFO, null, "ok"); 
    } 

    FacesContext.getCurrentInstance().addMessage("form:password", message); 
} 

任意のアイデアの男?

+0

あなたはそれがうまくいかないとはどういう意味ですか?それは遅いですか?あなたが何を変えたいのか、なぜそれを理解しているのか分かりません。 – jdias

+0

こんにちは@jdias、私はそれがあると思われる方法で動作していないことを意味します。おかげさまで、少し混乱するかもしれません。 –

答えて

19

まず、 Validatorを入力して入力を検証してください。アクションイベントメソッドでは実行しないでください。あなたの具体的な問題については

、あなただけの唯一の現在のコンポーネントに<f:ajax>、それはつまり、デフォルトのexecute属性でフィールドの両方を指定する必要があります。バリデーターを最初の入力に接続し、2番目の入力の値を<f:attribute>として送信すると、バリデーターで取り込むことができます。 binding属性を使用して、コンポーネントをビューにバインドすることができます。この方法で、提出された値をUIInput#getSubmittedValue()に渡すことができます。

はここでキックオフの例です:

<h:outputLabel for="password" value="Password:" /> 
<h:inputSecret id="password" value="#{bean.password}" required="true"> 
    <f:validator validatorId="confirmPasswordValidator" /> 
    <f:attribute name="confirm" value="#{confirmPassword.submittedValue}" /> 
    <f:ajax event="blur" execute="password confirm" render="m_password" /> 
</h:inputSecret> 
<h:message id="m_password" for="password" /> 

<h:outputLabel for="confirm" value="Password (again):" /> 
<h:inputSecret id="confirm" binding="#{confirmPassword}" required="true"> 
    <f:ajax event="blur" execute="password confirm" render="m_password m_confirm" /> 
</h:inputSecret> 
<h:message id="m_confirm" for="confirm" /> 

(また、私は両方のコンポーネントにrequired="true"を追加したことに注意してください、あなたは、必ずしもそれがだ、マネージドBeanのプロパティに確認パスワードの成分値をバインドする必要はありませんのでご注意しますこのバリデータで

)とにかく無益あそこ

@FacesValidator("confirmPasswordValidator") 
public class ConfirmPasswordValidator implements Validator { 

    @Override 
    public void validate(FacesContext context, UIComponent component, Object value) throws ValidatorException { 
     String password = (String) value; 
     String confirm = (String) component.getAttributes().get("confirm"); 

     if (password == null || confirm == null) { 
      return; // Just ignore and let required="true" do its job. 
     } 

     if (!password.equals(confirm)) { 
      throw new ValidatorException(new FacesMessage("Passwords are not equal.")); 
     } 
    } 

} 
+0

binding = "#{confirmPassword}"のconfirmPasswordは何ですか? –

+1

@ Jsword:これは、ビュー内の特定の識別子でビューにコンポーネントをバインドするので、ビューのどこかで '#{confirmPassword} '(例えば、最初のフィールドの' 'など)で利用できるようにバインドします。バリデーター)。 – BalusC

+0

私は今、ヘルプデュードのおかげで、 'バインディング' BalusCの機能を理解しています。 –

0

シーム2では、コンポーネント<s:validateEquality>があり、コードを書く必要はありません。 JSF2の場合、Seam 3モジュール、特にFaces moduleとクロスフィールドフォーム検証があります。例:

まずあなたがs:validateFormタグを使用する必要があります。

<h:form id="passwordForm"> 
    <h:inputSecret id="newPassword" 
     required="true" 
     redisplay="true" 
     value="#{passwordController.newPassword}"> 
    </h:inputSecret>    
    <h:inputSecret id="confirmationPassword" 
     value="#{passwordController.confirmPassword}" 
     required="true" 
     redisplay="true"> 
    </h:inputSecret> 
    <h:commandButton id="submit" value="Submit" action="#{passwordController.submitPassword}" /> 
    <s:validateForm validatorId="passwordValidator" /> 
</h:form> 

と上記パスワードフォームは次のようになりますため、対応する検証:すべての

@FacesValidator("PasswordValidator") 
public class PasswordValidator implements Validator 
{ 
    @Inject 
    @InputField 
    private String newPassword; 

    @Inject 
    @InputField 
    private String confirmPassword; 

    @Override 
    public void validate(final FacesContext context, final UIComponent comp, final Object values) throws ValidatorException 
    { 
     if (!confirmPassword.equals(newPassword)) 
     { 
     throw new ValidatorException(new FacesMessage("Passwords do not match!")); 
     } 
    } 
} 
+0

純粋なJSF2で同様のソリューションを探しましたが、見つけられませんでした。これがJSF2に存在するかどうか知っていますか?特に@InputFieldアノテーションは便利です。 –

0

あなたはuのできse Primefaces p:パスワードタグ。デモexampleをご覧ください。それはと一致するアトリビュートのパスワードを確認するIDです。

<p:panel header="Match Mode"> 
    <p:messages id="messages" showDetail="true" autoUpdate="true"/> 

    <h:panelGrid columns="2" id="matchGrid">      
       <h:outputLabel for="pass" value="Password " /> 
       <p:password id="pass" value="#{passwordBean.password}" match="confirmPass" required="true"/> 

       <h:outputLabel for="confirmPass" value="Confirm Password " /> 
       <p:password id="confirmPass" value="#{passwordBean.confirmPassword}" required="true"/> 
    </h:panelGrid> 

    <p:commandButton id="saveButton" update="matchGrid" value="Save" /> 
</p:panel> 
関連する問題