Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

required validation problem  RSS feed

 
Natt Gonzalez
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi!
I have two fields in my page. How can I do to make a required only for one of them? I want to send an error when the two fields are empty. When the field one is set by the user it must be correct or when the second field is set is also correct. But it is an error when any field is set.
I have thought to implement my own validator but it is only called when the field is set.
Is there a way to extend the required validation?
Thanks!!!
 
Tim Holloway
Bartender
Posts: 18531
61
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
All validators for a given form are invoked when that form is submitted. What you need is a validator that can cross-check items on a form.

I've done something similar for a common case, where you have to enter a password twice and I wanted the validator to confirm that the two passwords matched. If they didn't, I'd throw a validation exception. The validator was only defined for the second password textInput control, but it referenced the first password control.

In order to accomplish that, I had to make the validator walk the JSF view structure and read the first password field's contents so that the two passwords could be compared. In your case, you'd want to check to see if the primary text input was non-empty before validating the second one.

Incidentally, the MyFaces Extensions Validations may have something like what you want.
 
Natt Gonzalez
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Tom, Thanks for your answer but this is not my problem. I'm using JSF with IBM portal. My validator is only called when one of my fields is not empty. In your example I donĀ“t know if both passwords are empty if your validator is called. In my version of JSF it is the real problem.
Thanks
 
Tim Holloway
Bartender
Posts: 18531
61
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I repeat ALL validators are invoked when a form is submitted. For every field on that form. That's part of the JSF architecture, which is designed to ensure all-or-nothing data updating by forbidding mixing bad data in with good data.

Validators for any other forms on the page won't be invoked, because only one form gets submitted per HTTP request. That's also true in the case of portlets.
 
Natt Gonzalez
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Tom, Thanks for your answer but I'm not agree with you. I have been looking for that for so long, and today I have found this post.

http://jamiemcilroy.wordpress.com/2006/10/10/not-quite-what-i-expectedjsf-validation/

Thanks for helping me.
 
Tim Holloway
Bartender
Posts: 18531
61
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, that was interesting. And a little unsettling. I think their rationale was that since you indicate mandatory input via the "required='true'" attribute, it's not a prerequisite for validation.

Since you're looking for a conditional "required", one possibility is to bind a validator to the parent field instead of the dependent field. Since the whole point was to check the dependent field iff the parent is present.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!