Validating JSF passwords using ADF 11

How to create a validator that checks if the user enters the same values ​​in the password field and in the password confirmation field?

I did this in a managed bean, but I prefer to do it with JSF validation ...

The real question is: how to create a validator that will access other JSF components other than the component being checked?

I am using ADF Faces 11.

Thanks...

+3
source share
2 answers

The real question is: how to create a validator that will access other JSF components other than the component being checked?

Do not try to access components directly; You will regret it. The JSF validation engine works best to prevent spam from entering the model.

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).

+4

. - :

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" />

+2

Source: https://habr.com/ru/post/1718820/


All Articles