0

How do I implement cross-field validation for composite components? The composite component I am using is an input text box (one for email and the second one for confirm email). I applied f:validator tag for the confirmEmail component. How to obtain the value for email composite component in the validate method. Is it UIComponent or UINamingContainer?

Jacob
  • 77,566
  • 24
  • 149
  • 228
deepthi
  • 1
  • 2
  • possible duplicate of [JSF2.0 doesn't support cross-field validation, is there a workaround?](http://stackoverflow.com/questions/6282466/jsf2-0-doesnt-support-cross-field-validation-is-there-a-workaround) – Bo Persson Jul 03 '12 at 12:35

2 Answers2

0

Technically, your composite is a UINamingcontainer, but any component can find its children.

I suspect something like the following should work

public void validate(FacesContext context, UIComponent component, Object value) {
    UIInput first = (UIInput)component.findComponent("compositesFirstInputID");
    UIInput second = (UIInput)component.findComponent("compositessecondInputID");

    Object firstEntry = first.getSubmittedValue();
    Object secondEntry = second.getSubmittedValue();
    if(!firstEntry.equals(secondEntry))
        throw new ValidatorException(...);
}

Might want to add some null checking, possibly trim() and use equlasIgnoreCase.

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
JimO
  • 102
  • 1
  • 9
  • @deepti Misread your question - Thought you had a single composite with two inputs,and the using page's composite reference had a child f:validate. – JimO Mar 26 '11 at 05:25
0

I have implemented the validate method as discussed above. I think the problem with my code is where to use the f:validate tag on composite component.

<eg:inputText id="confirmEmail" value="backingbean.email"/>

<eg:inputText id="email" value="backingbean.email">
<f:validator validatorId="core.jsf.CompareValidator" for="inputText"/>
</eg:inputText>

But when I submit the form the validator is not being called. Should I wrap the validator around the component or is this the correct way of implementing.