2

In register.xhtml page, I have 2 inputText components for password and confirm password as following:

<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:ui="http://java.sun.com/jsf/facelets"
      xmlns:h="http://java.sun.com/jsf/html"
      xmlns:p="http://primefaces.prime.com.tr/ui"
      xmlns:f="http://java.sun.com/jsf/core">

   <h:form>

      <h:outputText style="font-weight: bold" value="Password: " />
      <p:password feedback="true" minLength="9" 
                  binding="#{mrBean.passwordComponent}"
                  id="password" value="#{mrBean.password}"/>
      <p:message for="password" id="passwordMsg" />

      <h:outputText style="font-weight: bold" value="Confirm password: " />
      <p:password feedback="false" minLength="9" 
                  id="confirmPassword" value="#{mrBean.confirmPassword}"
                  validator="#{mrBean.validateConfirmPassword}>
         <f:attribute name="oriPassword" value="#{mrBean.passwordComponent.submittedValue}"/>
         <p:ajax process="password confirmPassword" update="confirmPasswordMsg" /> 
      </p:password>
      <p:message for="confirmPassword" id="confirmPasswordMsg" />

   </h:form>

</html>

And this is my mrBean:

@ManagedBean
@RequestScoped
public class MrBean {

    private String  password;
    private String  confirmPassword;
    private UIInput passwordComponent;

    public void validateConfirmPassword(FacesContext context, UIComponent toValidate,
            Object value) throws ValidatorException {

        String passwordStr        = (String) toValidate.getAttributes().get("oriPassword");
        String confirmPasswordStr = (String) value;

        if (!confirmPasswordStr.equals(passwordStr)) {
            FacesMessage message = new FacesMessage("The 2 passwords do not match.");
            throw new ValidatorException(message);
        }
    }

}

In another page, I also have a similar bean with similar validate function for email & confirmEmail and it works perfectly. However, I have no idea why it couldn't work here. The passwordStr is always null even though I have already entered the password.

I'd be very grateful if someone could show me what I have done wrong here.

Best regards, James Tran

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
Mr.J4mes
  • 9,168
  • 9
  • 48
  • 90
  • Perhaps the validator method should be `validateConfirmPassword` and not `validateConfirmEmail`. Have you posted the right code? – Vineet Reynolds Dec 03 '11 at 11:29
  • @VineetReynolds: Thanks for correcting me :P. I typed the code wrongly. I have updated my question with the proper code. – Mr.J4mes Dec 03 '11 at 16:57

1 Answers1

1

JSF components are processed in the order they appear in the component tree. During validations phase, for each component the submitted value will be retrieved by getSubmittedValue(), converted and validated. If no exceptions occurred during conversion and validation, then the submitted value will be set to null and the converted and validated value will be set as local value by setValue().

You're trying to reference the submitted value of the component which has already been processed at that point. The submitted value will only be non-null when conversion/validation failed for that value. You need to reference its local value instead.

<f:attribute name="oriPassword" value="#{mrBean.passwordComponent.value}"/>

See also:

Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555