Используйте нормальный Validator
и передайте значение первого компонента в качестве атрибута второго компонента.
<h:inputSecret id="password" binding="#{passwordComponent}" value="#{bean.password}" required="true"
requiredMessage="Please enter password" validatorMessage="Please enter at least 8 characters">
<f:validateLength minimum="8" />
</h:inputSecret>
<h:message for="password" />
<h:inputSecret id="confirmPassword" required="#{not empty passwordComponent.value}"
requiredMessage="Please confirm password" validatorMessage="Passwords are not equal">
<f:validator validatorId="equalsValidator" />
<f:attribute name="otherValue" value="#{passwordComponent.value}" />
</h:inputSecret>
<h:message for="confirmPassword" />
( обратите внимание, что binding
в приведенном выше примере является как есть; вам не следует связывать его со свойством bean-компонента! )
с
@FacesValidator(value="equalsValidator")
public class EqualsValidator implements Validator {
@Override
public void validate(FacesContext context, UIComponent component, Object value) throws ValidatorException {
Object otherValue = component.getAttributes().get("otherValue");
if (value == null || otherValue == null) {
return; // Let required="true" handle.
}
if (!value.equals(otherValue)) {
throw new ValidatorException(new FacesMessage("Values are not equal."));
}
}
}
Если вы используете библиотеку утилит JSF OmniFaces , тогда вы можете использовать <o:validateEquals>
для этого.Точный случай «подтвердить пароль» демонстрируется на <o:validateEqual>
витрина .