PEAR Forum :: PHP Extension and Application Repository

PEAR Forum Forum Index
 FAQFAQ   SearchSearch   MemberlistMemberlist   RegisterRegister   ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 
QuickForm - dynamic rules depending on checked radios

 
Post new topic   Reply to topic    PEAR Forum Forum Index -> HTML
View previous topic :: View next topic  
Author Message
aicos



Joined: 22 May 2007
Posts: 10

PostPosted: Mon Jul 09, 2007 2:10 am    Post subject: QuickForm - dynamic rules depending on checked radios Reply with quote

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
View user's profile Send private message
mark



Joined: 07 Jan 2007
Posts: 1230

PostPosted: Mon Jul 09, 2007 10:19 am    Post subject: Re: QuickForm - dynamic rules depending on checked radios Reply with quote

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
View user's profile Send private message
aicos



Joined: 22 May 2007
Posts: 10

PostPosted: Mon Jul 09, 2007 11:45 am    Post subject: Reply with quote

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
View user's profile Send private message
mark



Joined: 07 Jan 2007
Posts: 1230

PostPosted: Mon Jul 09, 2007 1:34 pm    Post subject: Reply with quote

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
View user's profile Send private message
aicos



Joined: 22 May 2007
Posts: 10

PostPosted: Wed Jul 11, 2007 4:55 pm    Post subject: Reply with quote

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
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    PEAR Forum Forum Index -> HTML All times are GMT + 2 Hours
Page 1 of 1

 
Jump to:  
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



PEAR Forum topic RSS feed 
Powered by phpBB © 2001, 2005 phpBB Group

Provided by Ministry of Web developement