tags:

views:

26

answers:

1

Hello,

As we have comparevalidator in Asp.Net, what do we have in JSF to validate whether two field's value are same or not? I want to validate password and confirmPassword field.

+2  A: 

No, such a validator does not exist in the basic JSF implementation. You basically need to run the validator on the last component of the group and grab the other component you'd like to validate as well using UIViewRoot#findComponent(). E.g.

public void validate(FacesContext context, UIComponent component, Object value) {
    UIComponent otherComponent = context.getViewRoot().findComponent("otherClientId");
    Object otherValue = ((UIInput) otherComponent).getValue();
    // ...
}

Also see this article for more background info and concrete examples.

On the other hand, if you're already on JSF2, then you can also make use of ajaxical validation:

<h:form>
    <f:event type="postValidate" listener="#{bean.validate}" />
     ...
</h:form>

..where #{bean.validate} method look like this:

public void validate(ComponentSystemEvent e) {
    UIComponent form = e.getComponent();
    UIComponent oneComponent = form.findComponent("oneClientId");
    UIComponent otherComponent = form.findComponent("otherClientId");
    // ...
} 

Also see this article for more JSF2 validation examples.

BalusC