*
The moose likes JSF and the fly likes JSF: simple Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » JSF
Bookmark "JSF: simple "required" validation problem" Watch "JSF: simple "required" validation problem" New topic
Author

JSF: simple "required" validation problem

Javoso Torso
Ranch Hand

Joined: Aug 15, 2003
Posts: 88
Good Morning!

I have a required validation on a inputText field. If I submit without having inserted a char or a number there, it notifies the error and does not commit. And thats all ok and what I want to have.

My problem is if I want to cancel (go back to the main page) or reset (clear all giving entries and restoring the olds). If I was editing the field (so: load state was field having an entry) and let it empty: I cannot cancel or reset. I cannot go further if I do not insert something there.

By cancel or reset I would like to continue and not being forced to input something!

I had it using Struts. How is the way with JSF?

Thanks for any solution!

Regards,
Javo


PS: I realized to late that there is a special forum for JSF...
Can someone move this there?
[ January 24, 2005: Message edited by: Javoso Torso ]

I hate 'trial and error'<br />and I am doing it all the time
Jason Menard
Sheriff

Joined: Nov 09, 2000
Posts: 6450
Moved to JSF.
Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15299
    
    6

Add the attribute immediate=true to your inputText. Then when you hit cancel it won't perform the validation. You might want to take a look at this and this for some interesting issues I had when using the immediate attribute though. But for what you want, it should work perfectly.


GenRocket - Experts at Building Test Data
Javoso Torso
Ranch Hand

Joined: Aug 15, 2003
Posts: 88
I try immediate="true"


And the prob then is that I also use javaScript while cancelling.


Even that js call is being avoid if you use immediate...


so: I need to redesign...
Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15299
    
    6

Originally posted by Javoso Torso:
I try immediate="true"


And the prob then is that I also use javaScript while cancelling.


Even that js call is being avoid if you use immediate...


so: I need to redesign...


Good luck. Basically what I did is just some manual server side validation. When my action is triggered the first thing I do is validate my textfield. I'll return a FacesMessage with the error message. Just don't put any validation code in your cancel/reset action. That's really about the only way that I know of.
Varun Khanna
Ranch Hand

Joined: May 30, 2002
Posts: 1400

By cancel or reset I would like to continue and not being forced to input something!
I had it using Struts. How is the way with JSF?

[/QB]


Have you tried putting immediate="true" in your Reset and Cancel button "only".

It doesn't works for me (but theortically it should work).
I too use javascript to erase the values of the fields having validation

For "Cancel", there is another "silly" approach.
Put your Cancel button in different form, so when you will click it, only that blank form will be submitted and since no component is present, there won't be any validation

[But I will advise to first try doing reset and cancel with immediate=true only]
[ January 24, 2005: Message edited by: K Varun ]

- Varun
Varun Khanna
Ranch Hand

Joined: May 30, 2002
Posts: 1400
Originally posted by Gregg Bolinger:
Add the attribute immediate=true to your inputText. Then when you hit cancel it won't perform the validation.


Here you are ensuring that validation "must" take place before submission

immediate=true ensures events,conversion,validation should take place in APP_REQ_PHASE only for the component it is attached to and thereafter it moves to update model phase.
In short, that component goes through normal life cycle.

Other component doesnt goes through validation phases, hence, since their validation isn't done, they dont go to updateModel phase OR any of the later phases.
[ January 24, 2005: Message edited by: K Varun ]
Javoso Torso
Ranch Hand

Joined: Aug 15, 2003
Posts: 88
it isn�t funny? the required="true" should simplify programmers life and here are we discussing 1000 of possibilities to get it work...

I also have now some "own" validations on the server side

even if I find the solution with the second form very funny ... hacky

All these frameworks should allow to have clean code and what happen: we hack around

Regards,
Javo

PS: This will become my signature now:
I hate to hack
I hate try and error
[ January 24, 2005: Message edited by: Javoso Torso ]
Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15299
    
    6

Originally posted by K Varun:


Here you are ensuring that validation "must" take place before submission

immediate=true ensures events,conversion,validation should take place in APP_REQ_PHASE only for the component it is attached to and thereafter it moves to update model phase.
In short, that component goes through normal life cycle.

Other component doesnt goes through validation phases, hence, since their validation isn't done, they dont go to updateModel phase OR any of the later phases.

[ January 24, 2005: Message edited by: K Varun ]



CRAP! I meant to use immediate on the cancel button. Sorry about that.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: JSF: simple "required" validation problem
 
Similar Threads
Newbie no validation on Cancel button click.
How can I refresh(reset) inputText in a dataTable?
Reset the form to Null or Reload the page using reset even after a submit
Is Backing bean Required
How to recognize the ActionEvent in a FocusLost method??