Validating against 2 fields
qwertywin Jun 13, 2009 7:34 PMOk so I have been trying in vain for a day now to get this to work, Im trying to validate my 2 password fields, but all the example I try end up with bogus errors, though the root is probably that I just dont understand what is going on/wrong
so far I have
<s:decorate id="passwordField" template="/layout/edit.xhtml"> <ui:define name="label">Password</ui:define> <h:inputSecret id="password" required="true" size="45" maxlength="45" value="#{memberHome.instance.password}" validator="#{memberValidator.validatePassword}" > <f:attribute name="passwordId" value="memberProfile:confirmPassword" /> <f:validateLength minimum="6"/> <a:support event="onblur" reRender="passwordField" bypassUpdates="true" ajaxSingle="true" /> </h:inputSecret> </s:decorate> <s:decorate id="confirmPasswordField" template="/layout/edit.xhtml"> <ui:define name="label">Confirm Password</ui:define> <h:inputSecret id="confirmPassword" required="true" size="45" maxlength="45" value="#{memberPasswordValidator.confirmPassword}" validator="#{memberValidator.validatePassword}" > <f:attribute name="passwordId" value="memberProfile:password" /> <f:validateLength minimum="6"/> <a:support event="onblur" reRender="passwordField" bypassUpdates="true" ajaxSingle="true"/> </h:inputSecret> </s:decorate>
and in my component
@Name(value="memberValidator") @BypassInterceptors public class MemberValidator { public void validatePassword(FacesContext context, UIComponent component, Object value) throws ValidatorException { // Obtain the client ID of the first password field from f:attribute. String passwordId = (String) component.getAttributes().get("passwordId"); // Find the actual JSF component for the client ID. UIInput passwordInput = (UIInput) context.getViewRoot().findComponent(passwordId); if( passwordInput != null ){ // Get its value, the entered password of the first field. String password = (String) passwordInput.getValue(); // Cast the value of the entered password of the second field back to String. String confirm = (String) value; // Compare the first password with the second password. if (!password.equals(confirm)) { throw new ValidatorException(new FacesMessage("Passwords are not equal.")); } // You can even validate the minimum password length here and throw accordingly. // Or, if you're smart, calculate the password strength and throw accordingly ;) } } private String confirmPassword; public void setConfirmPassword(String confirmPassword) { this.confirmPassword = confirmPassword; } public String getConfirmPassword() { return confirmPassword; } }
If someone could steer me in the right direction that'd be great.