tags:

views:

75

answers:

2

If I have many input controls in a form (There are separate validators for each of these input controls - like required,length and so on ) , there is a command button which submits the form and calls an action method. The requirement is - though the input control values are , say , individually okay - the combination of these values should be okay to process them together after the form submission - Where do i place the code to validate them together?

1) Can i add a custom validator for the command button and validate the combination together? like validate(FacesContext arg0, UIComponent arg1, Object value) but even then I will not have values of the other input controls except for the command button's/component's value right ?

2) can i do the validation of the combination in the action method and add validation messages using FacesMessage ?

or do you suggest any other approach?

Thanks for your time.

+1  A: 

I've successfully used the 2nd approach:

FacesMessage facesMessage = 
      new FacesMessage(FacesMessage.SEVERITY_ERROR, msg, msg);
FacesContext.getCurrentInstance().addMessage(null, facesMessage);
Bozho
Thanks Bozho. I would prefer to use the second approach.
gurupriyan.e
+1  A: 

Point 2 is already answered by Bozho. Just use FacesContext#addMessage(). A null client ID will let it land in <h:messages globalOnly="true">. A fixed client ID like formId:inputId will let it land in <h:message for="inputId">.

Point 1 is doable, you can grab the other components inside the validator method using UIViewRoot#findComponent():

UIInput otherInput = (UIInput) context.getViewRoot().findComponent("formId:otherInputId");
String value = (String) otherInput.getValue();

You however need to place f:validator in the last UIInput component. Placing it in an UICommand component (like the button) won't work.

True, hardcoding the client ID's is nasty, but that's the payoff of a bit inflexible validation mechanism in JSF.

BalusC
How do i stop the execution after the messages are added as per the second approach ?
gurupriyan.e
Return to the same page.
BalusC