Re: Quickform group rules
| From: | Alexey Borzov | Date: | Sun, 08 Nov 2009 08:51:47 +0000 |
| Subject: | Re: Quickform group rules | ||
| References: | 1 | Groups: | php.pear.general |
| Request: | Send a blank email to pear-general+get-30127@lists.php.net to get a copy of this message | ||
Hi Alain,
Alain Williams wrote:
I have a form in which there is an Email and a Phone field. I want to ensure that I have at least one of these set, I also want to apply some validation to the 2 fields. Even if both fields are filled in it complains about not enough. Can someone please tell me where I am going wrong. BTW: I am using HTML/QuickForm.php on PHP5 (I have had to patch lots of things to get rid of the warnings)Sure you haven't broken anything?
$contact[] = $form->addElement('text', 'Email', 'Email', 'size=50');
Here you should have used createElement()
$form->addRule('Email', 'Invalid email address', 'email', null, 'client');
That should be done via addGroupRule() after adding the group.
$contact[] = $form->addElement('text', 'Phone', 'Phone', 'size=20');
$form->addRule('Phone', 'Invalid phone number', 'regex', '/^[0+][\d ]+$/', 'client');
$form->addGroup($contact, 'contact');
$form->addGroupRule('contact', 'You must enter at least one of Phone number or Email address', 'required', null, 1, 'client');
Basically you've added a couple of fields to a form via addElement() and then added the same fields to a group *again*. I'm not sure that HTML_QuickForm is smart enough to detect and handle that.
Consider using HTML_QuickForm2 instead, especially if you want to get rid of E_STRICT error messages. It *will* detect such things and won't add one element to two containers, it also attaches Rules to elements rather than to a form so it's a bit more intuitive and throws Exceptions in case of programming errors: these are a bit more difficult to ignore than PEAR_Errors.