• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Rob Spoor
  • Henry Wong
  • Liutauras Vilda
Saloon Keepers:
  • Tim Moores
  • Carey Brown
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
Bartenders:
  • Frits Walraven
  • Himai Minh
  • Jj Roberts

Conditional Validation Determined By Which Button Was Clicked

 
Ranch Hand
Posts: 77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Ranch Hand
Posts: 114
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
(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
Posts: 114
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
I don't get it. A whale wearing overalls? How does that even work? It's like a tiny ad wearing overalls.
SKIP - a book about connecting industrious people with elderly land owners
https://coderanch.com/t/skip-book
reply
    Bookmark Topic Watch Topic
  • New Topic