Using the form validator with at least one field required

281 views
Skip to first unread message

Olivier Girardot

unread,
Aug 13, 2011, 7:33:22 AM8/13/11
to mootool...@googlegroups.com
Hi everyone, 
i'm using the Form.Validator.Inline greatly for my forms, but now i need to specify a validation condition for two fields like "at least one is required", 
i didn't find anything in the documentation, as it seems that validation using class attributes is only on a per-field basis.
Is there any way to do it using Form.Validator.Inline ?
Thanks everyone, 

Regards.
Olivier.

Aaron Newton

unread,
Aug 13, 2011, 11:42:48 PM8/13/11
to mootool...@googlegroups.com
There is already a validator for this. Note that there seems to be something wrong with the docs for the form validator (the parser is breaking on something), so I can't easily link to it, but you can read about it on github's parsed version:


Search for validate-one-required

If that doesn't suit your needs, write your own custom validator; they're pretty easy to write.

Olivier Girardot

unread,
Aug 14, 2011, 3:55:32 AM8/14/11
to mootool...@googlegroups.com
Thanks Aaron, that's what i did, :

Form.Validator.add('validate-at-least-one-required', {
   errorMsg: 'At least one field is required',
   test: function(element, value){        
       if (element.value.length == 0 && value['validate-at-least-one-required'].value.length == 0) return false;
       else return true;
   }
});

using it like that :
<li><textarea id="positifComment" name="positif_comment" class="validate-at-least-one-required:negatifComment" rows="5" cols="30"></textarea></li> 
<li><textarea id="negatifComment" name="negatif_comment" class="validate-at-least-one-required:positifComment"rows="5" cols="30"></textarea></li>

But what you showed seems to be exactly what i needed :)
Thanks, 

Olivier.

2011/8/14 Aaron Newton <aa...@iminta.com>

Aaron Newton

unread,
Aug 14, 2011, 3:34:57 PM8/14/11
to mootool...@googlegroups.com
Well, the validator I sent you looks for a shared DOM parent, so your html below wouldn't work. But that's ok; one of the nice things about the validator code is how easy it is to write your own. Seems like you have a solution that works.

Olivier Girardot

unread,
Aug 14, 2011, 3:56:07 PM8/14/11
to mootool...@googlegroups.com
yeah thx, i'll keep the other one in mind next time, or when the condition will become more complicated.
By the way i didn't find any way to pass more than one argument to the validator. 
something like :
validate-at-least-one-required:{id: negatifComment, type:shared} 


2011/8/14 Aaron Newton <aa...@iminta.com>

Aaron Newton

unread,
Aug 14, 2011, 8:11:36 PM8/14/11
to mootool...@googlegroups.com
You can specify any number of values in the validator-props; they all get passed.

Olivier Girardot

unread,
Oct 5, 2011, 6:52:28 AM10/5/11
to mootool...@googlegroups.com
Hi back everyone, 
Like you said Aaron i thought i had a solution that worked, but the "value" object i'm using seems to be only available under Chrome
When i test it on firefox the validator fails and the form gets POSTed 
i think i'll try to use the validator you showed me in the doc, but in the meantime 
any idea about why this behaviour on Firefox ... ?
Thanks again. 

2011/8/14 Aaron Newton <aa...@iminta.com>

Aaron Newton

unread,
Oct 5, 2011, 12:13:08 PM10/5/11
to mootool...@googlegroups.com
Fiddle it. Off the top of my head, no.

Olivier Girardot

unread,
Oct 5, 2011, 3:46:58 PM10/5/11
to mootool...@googlegroups.com
i fiddled it http://jsfiddle.net/h6ftB/1/
finally i figured it out, it was mainly an issue the way i used my validators and the way the props variable was filled accross browsers.
Thanks again.

Olivier.

2011/10/5 Aaron Newton <anu...@gmail.com>
Reply all
Reply to author
Forward
0 new messages