3

ユーザーがパスワードフィールドとパスワード確認フィールドに同じ値を入力したかどうかを検証するバリデーターを作成するにはどうすればよいですか?

マネージドBeanで実行しましたが、JSFバリデーターを使用して実行することを好みます...

本当の問題は、検証対象のコンポーネント以外の他のJSFコンポーネントにアクセスするバリデーターを作成する方法です。

ADFFaces11を使用しています。

ありがとう...

4

2 に答える 2

4

本当の問題は、検証対象のコンポーネント以外の他のJSFコンポーネントにアクセスするバリデーターを作成する方法です。

コンポーネントに直接アクセスしようとしないでください。あなたはそれを後悔するでしょう。JSFの検証メカニズムは、ジャンクがモデルに侵入するのを防ぐのに最適です。

別のタイプのマネージドBeanを使用できます。何かの形:

/*Request scoped managed bean*/
public class PasswordValidationBean {
  private String input1;
  private String input2;
  private boolean input1Set;

  public void validateField(FacesContext context, UIComponent component,
      Object value) {
    if (input1Set) {
      input2 = (String) value;
      if (input1 == null || input1.length() < 6 || (!input1.equals(input2))) {
        ((EditableValueHolder) component).setValid(false);
        context.addMessage(component.getClientId(context), new FacesMessage(
            "Password must be 6 chars+ & both fields identical"));
      }
    } else {
      input1Set = true;
      input1 = (String) value;
    }
  }
}

これは、メソッドバインドメカニズムを使用してバインドされます。

<h:form>
  Password: <h:inputSecret
    validator="#{passwordValidationBean.validateField}"
    required="true" />
  Confirm: <h:inputSecret
    validator="#{passwordValidationBean.validateField}"
    required="true" />
  <h:commandButton value="submit to validate" />
  <!-- other bindings omitted -->
  <h:messages />
</h:form>

将来的には、Bean Validation( JSR 303 )を使用してこの種のことを実行できるようになるはずです。

于 2009-09-29T17:03:54.447 に答える
2

コンテキストマップから他のフィールドの値をいつでも取得して、複数のフィールドで検証を実行できます。以下のようなもの:

public void  validatePassword2(FacesContext facesContext, UIComponent uIComponent, Object object) throws ValidatorException{
    String fieldVal = (String)object;

    String password1 = (String)FacesContext.getCurrentInstance().getExternalContext().getSessionMap().get("password1");
    if(!password1.equalsIgnoreCase(fieldVal)){
        FacesMessage message = new FacesMessage("Passwords must match");
        throw new ValidatorException(message);
    }
}   

jsfは次のようになります。

<h:inputSecret value="#{profile.password2}" validator="#{brokerVal.validatePassword2}" required="true" requiredMessage="*required" id="password2" />

HTH

于 2009-12-28T21:13:51.173 に答える