 |
|
|
| View previous topic :: View next topic |
| Author |
Message |
aicos
Joined: 22 May 2007 Posts: 10
|
Posted: Mon Jul 09, 2007 2:10 am Post subject: QuickForm - dynamic rules depending on checked radios |
|
|
Hi.
I'm trying to figure out how to setup the rules for the following:
Lets say I have 4 radios and a textbox. If one of the first 3 radios is filled in then the textbox requires no text. If the forth radio is filled in then the textbox requires some text.
Putting all the elements inside a group and adding a required rule doesn't work (yes I was silly enough to try that).
Any suggestions?
Thanks. |
|
| Back to top |
|
 |
mark

Joined: 07 Jan 2007 Posts: 1230
|
Posted: Mon Jul 09, 2007 10:19 am Post subject: Re: QuickForm - dynamic rules depending on checked radios |
|
|
| aicos wrote: | I'm trying to figure out how to setup the rules for the following:
Lets say I have 4 radios and a textbox. If one of the first 3 radios is filled in then the textbox requires no text. If the forth radio is filled in then the textbox requires some text.
Putting all the elements inside a group and adding a required rule doesn't work (yes I was silly enough to try that). |
Using a group is not wrong here. But you need to define your own (group) rule that checks this special case. You can define both a server- (needed) and a client-side rule. Examples for such custom roles should be availabe in the manual. |
|
| Back to top |
|
 |
aicos
Joined: 22 May 2007 Posts: 10
|
Posted: Mon Jul 09, 2007 11:45 am Post subject: |
|
|
Are you saying that I *must* use some client validation to make this work?
I couldn't find any examples that are similar to what I am trying to achieve. Can you point me to the relevant section?
Thanks |
|
| Back to top |
|
 |
mark

Joined: 07 Jan 2007 Posts: 1230
|
Posted: Mon Jul 09, 2007 1:34 pm Post subject: |
|
|
| aicos wrote: | | Are you saying that I *must* use some client validation to make this work? |
No, the other way round: You need a server-side rule, but you *can* also add a custom client-side rule.
| aicos wrote: | | I couldn't find any examples that are similar to what I am trying to achieve. Can you point me to the relevant section? |
You could follow the checkEmail() example on this page:
http://pear.php.net/manual/en/package.html.html-quickform.intro-validation.php
But I'm not sure whether this also works in similar way for group rules.
Another solution would be to use addFormRule(): This allows very customized rules. There is an example available in formrule.php in the docs/examples/ directory. |
|
| Back to top |
|
 |
aicos
Joined: 22 May 2007 Posts: 10
|
Posted: Wed Jul 11, 2007 4:55 pm Post subject: |
|
|
Ok, solved it. Thanks for the help and here is the simplified solution for those interested:
| Code: |
function checkother($elements)
{
if ($elements[0] == 'radio4')
{
if ($elements[1] =='')
return false;
}
return true;
}
registerRule('othercheck','callback','checkother');
addRule(array($questionGroup,'radio_text'),'Please specify other','othercheck');
|
The actual code is more complicated than this, so I left out the lines that add the radios and textbox. But you can assume that there is one group ($questionGroup) containing just 4 radios. This is added to a required rule. Then there is just one textbox ('radio_text') and the custom rule that you can see above. |
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|