Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

how to clear the values of components on validation errors in JSF

 
Sumeet Singh Aryan
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi,
I have implemented a form in JSF with 10 components (combo box, text
boxes etc.). if any of the validation is failed in these components
then i want to clear the value of one particular component. e.g if
combo box is not selected then i want to clear one of the text boxes

Thanks in Advance
 
Bauke Scholtz
Ranch Hand
Posts: 2458
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Run a PhaseListener which listens on beforephase of render response and sets the submitted values of all UIInput components to null.
 
Sumeet Singh Aryan
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But the code of phaseListener will execute on each and every request. i want to clear particular textbox on the single page not on all pages but at some validation error generated by another components
 
Bauke Scholtz
Ranch Hand
Posts: 2458
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just check if Iterator#hasNext() of FacesContext#getMessages() returns true and then change the component tree accordingly. If necessary also check the UIViewRoot#getViewId() if it matches the desired view.
 
Mansi Mishra
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sumeet Aryan wrote:But the code of phaseListener will execute on each and every request. i want to clear particular textbox on the single page not on all pages but at some validation error generated by another components


You could try a javascript function
 
Bauke Scholtz
Ranch Hand
Posts: 2458
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That does not clear the values in the managed bean.
 
Sumeet Singh Aryan
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Where to write the code of checking getMessages(). i can't write in submit button. my validations are occured after submission
 
Bauke Scholtz
Ranch Hand
Posts: 2458
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As said, in the PhaseListener.
 
Sumeet Singh Aryan
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But how I came to know that the messages are coming from that particular page in which i want to clear the textbox
 
Bauke Scholtz
Ranch Hand
Posts: 2458
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As said, if necessary check the view id.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic