Conditional Validation on Button Click

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.


Comments:

It's better to perform redirect-after-post once validation is failed. So I wouldn't suggest to return "null" as the outcome. It might be better to return some preconfigured navigation case from the config which has <redirect/> flag triggered. I faced with an issue which is related to this one a little bit. I need an UIComponent to perform redirect-after-post when its embedded "required" validator fails (it happens when you set required=true to the component) I haven't found any solution yet.

Posted by Nikolay on May 20, 2007 at 06:39 PM 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 02, 2008 at 04:17 PM 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 03, 2008 at 09:11 PM PST #

Post a Comment:
  • HTML Syntax: NOT allowed
About

jayashri

Search

Categories
Archives
« April 2014
SunMonTueWedThuFriSat
  
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
   
       
Today