aspose file tools*
The moose likes JSF and the fly likes required field...but only if enabled Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSF
Bookmark "required field...but only if enabled" Watch "required field...but only if enabled" New topic
Author

required field...but only if enabled

Dave Wood
bronco
Ranch Hand

Joined: Aug 02, 2004
Posts: 161
This seems like it should be SO simple...

I've got a checkbox and a text field. When the checkbox is checked, the field gets enabled; uncheck it, field gets disabled.

I want the text field to be required only when the box is checked.

What seems to happen is this:

1. Checkbox initially disabled.
2. Check the box; field is enabled.
3. Submit the form.
4. Get validation error (good!)
5. Uncheck the box; field is disabled.
6. Submit the form.
7. STILL get the validation error (NOT GOOD! ...since the field is disabled)

I'm hoping I'm just having a brain-is-ready-for-the-weekend day and there's a simple fix for this. Suggestions?

[Oh, possibly of interest...I'm handling the enable/disable via javascript...here's the JSF code:


Thanks.


Co-Author of <a href="http://www.oreilly.com/catalog/jswing2" target="_blank" rel="nofollow">Java Swing</a><br />Co-Creator of <a href="http://www.sun.com/training/catalog/courses/CX-310-055.xml" target="_blank" rel="nofollow">SCJP 5.0</a> and <a href="http://www.sun.com/training/certification/java/associate_beta.xml" target="_blank" rel="nofollow">SCJA</a> exams
Varun Khanna
Ranch Hand

Joined: May 30, 2002
Posts: 1400
Originally posted by Dave Wood:

7. STILL get the validation error (NOT GOOD! ...since the field is disabled)


How did you disabled the field ...
Using javascript or JSF ?
[ January 14, 2005: Message edited by: K Varun ]

- Varun
Varun Khanna
Ranch Hand

Joined: May 30, 2002
Posts: 1400
Oops, I missed your post comment "the enable/disable via javascript":

But did you my question provided any hint
You disabled the field using javascript and didn't bother to inform Server and now you are expecting Server to know your component's state
Not fair




[ January 14, 2005: Message edited by: K Varun ]
Dave Wood
bronco
Ranch Hand

Joined: Aug 02, 2004
Posts: 161
...hmm, but its initial disabled state was also set by javascript and it seemed to do the right thing (ignoring the disabled field) in that case. So I'm still a bit confused.

(I have since reworked the thing to do the validation in the bean's action method...which is fine, I suppose)
Varun Khanna
Ranch Hand

Joined: May 30, 2002
Posts: 1400
Originally posted by Dave Wood:
...hmm, but its initial disabled state


I couldn't understand your initial 2 points:
1. Checkbox initially disabled.
2. Check the box; field is enabled.

How did u checked the box in step 2, which was disabled in step -1

anyways, if you are playing around with component's state in front end @javascript, do ensure same is reflected in server state also.
When you submit any page, the server component behaves as per the state information they have in store.

Check out Restore_View phase description@jsf_specification.
Sandip Chaudhuri
Greenhorn

Joined: Dec 27, 2004
Posts: 26
How to change the component state via jsf?
Do i have to submit the page?

If i do state changes by javascript how do I inform the server then?

Another question which is bothering me is that i have used validator="#{mybean.validateRequiredIf}" when the required="true" is not set for the component.

However if i do not enter any value then the validateRequiredIf is not called.
I am completely new to JSF and these are basics i guess.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: required field...but only if enabled