Conditional Validation
In your JSF application built using Creator, you might run into a
scenario where you want to execute validators only upon button click
and not when form submission happens via JavaScript
(for example when turn on autosubmit on change on a drop down list). In
this case you won't be able to attach any validators on your input
components because once you attach them, they will always get executed
during ProcessValidations
phase irrespective of whether the postback happened via button click or
form submission. So the work around is to do conditional validation
upon button click. So in your button's action handler, you validate
your input field and if it failed, return "null" as outcome from your
action handler to tell the JSF framework to redisplay the same page
instead of processing the navigation rules for the current view.
Here is the sample code you can add to your action handler to ensure
that textfield is not null when the page is submitted via button click.
if (textField1.getValue() == null) {
FacesMessage message = new FacesMessage("Textfield1 is required. Enter a value");
message.setSeverity(FacesMessage.SEVERITY_ERROR);
getFacesContext().addMessage(textField1.getClientId(getFacesContext()), message);
return null;
}
return "success";
Also, see David Botteril's blog on this topic for more alternatives.
Posted by Nikolay on May 21, 2007 at 01:39 AM PDT #
Hi, Can you help me
I have a problem with validation controls.I have more than 20 controls in my form along with 2 radio buttons.
If we click on one radio button some controls visable is true and remaining controls invisable all controls having validation controls.If we click on another radio button it will act vice versa of 1st radio button.Both radio button having comman button contols.
If I click on button then validation controls of invisable controls will be fired and button event not fired.
I want to that validation controls casus validations false how can I do this??
Posted by Suresh on March 03, 2008 at 12:17 AM PST #
Hi,
I think it is a better approach not to do it in the button action, but to use a custom validator method, in which you can check the source of the event being processed in the current request. (You can get this from the request parameter map.) This way validation is still a normal JSF validation and happens in Process Validations phase.
Regards,
Patrik
Posted by Patrik on December 04, 2008 at 05:11 AM PST #