File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes JSF and the fly likes Conditional Validation Determined By Which Button Was Clicked Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » JSF
Bookmark "Conditional Validation Determined By Which Button Was Clicked" Watch "Conditional Validation Determined By Which Button Was Clicked" New topic
Author

Conditional Validation Determined By Which Button Was Clicked

Wally Hartshorn
Ranch Hand

Joined: Jan 30, 2003
Posts: 77
I have a form with numerous input fields (laid out like a spreadsheet) and 2 buttons, "Save" and "Submit".

When the user clicks "Save", a hidden field at the end of the form triggers a custom validator, which examines the values in the input fields. It adds up the values in each column, comparing them to the expected total for that column. Any columns that have values must be correct. Any columns that are empty are ignored.

When the user clicks "Submit", I would like the same validations to run. However, at that time, empty columns would trigger validation errors.

My problem is that I can't figure out how the custom validator can determine which button was submitted, "Save" or "Submit".

The "Save" button has an action of "#{myForm.save}", while the "Submit" button has an action of "${myform.submit}". As a result, different methods are run when the buttons are clicked, but by then it is too late to do any further validation, right?

Any ideas?


Wally Hartshorn
A. Dusi
Ranch Hand

Joined: Sep 27, 2004
Posts: 114
Here is a possible workaround: do the validation in the action method as its first step instead of using a hidden field and a custom validator.
If the validation passed proceed, else return an empty String so the user sees the same page with error messages.




You can use same approach for actionListener too. Instead of returning a String, just say return;
Wally Hartshorn
Ranch Hand

Joined: Jan 30, 2003
Posts: 77
(For those just joining this thread: If the user clicks "Save", certain validations are optional, but if the user clicks "Submit", all validations must pass.)

I can do the check as part of the method being executed as a result of clicking the "Submit" button, but because we're no longer in the validation phase at that point, there's no way for me to add a FacesMessage to the FacesContext (so far as I know).

Is there no way for a custom validation method to determine which button was clicked on the form? If nothing else, the FacesContext must know what action is to be performed after validation is complete. Is there any way to get access to this information?

Anyone have any ideas?
A. Dusi
Ranch Hand

Joined: Sep 27, 2004
Posts: 114
I can do the check as part of the method being executed as a result of clicking the "Submit" button, but because we're no longer in the validation phase at that point, there's no way for me to add a FacesMessage to the FacesContext (so far as I know).


You still can add a FacesMessage in your action. Have you tried it? If you are using an action instead of actionListener, do not do a <redirect/> in your navigation rules. This will suppress the messages previously added.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Conditional Validation Determined By Which Button Was Clicked