Re: QF 3.2 - regex throwing errors

From: Date: Thu, 06 Nov 2003 10:09:50 +0000
Subject: Re: QF 3.2 - regex throwing errors
References: 1  Groups: php.pear.general 
Request: Send a blank email to pear-general+get-8807@lists.php.net to get a copy of this message
<mikeatwork@ukonline.co.uk> wrote : > > >> -----Original Message----- >> From: Alexey Borzov [mailto:borz_off@cs.msu.su] >> >> Hi! >> >> Mike Carter wrote: >>> Having attempted to switch to using addGroupRule() it seems to have >>> caused more problems. The on-line manual exmaple is different from >>> example7 in the archive. However I think I've implemented it the >>> same... >>> >>> $postcode[] = &HTML_QuickForm::createElement('text', >> 'postcode', '', >>> array('size' => '25', 'maxlength' => >>> '10', 'tabindex' => 2)); >>> $postcode[] = &HTML_QuickForm::createElement('static', 'help', >>> '', >>> $this->language['L_HelpPostcode'], $helpButton)); >>> $this->addGroup($postcode, 'postcodeGroup', >>> $this->language['L_YourPostcode']); >>> >>> // --- Form field rules >>> $this->addGroupRule('postcodeGroup', array('postcode' => >>> array( >>> array($this->language['L_RequirePostcode'], 'required', null, >>> 'client'), >>> array($this->language['L_InvalidPostcode'], 'regex', >>> '/[a-zA-Z]{1,2}[0-9][0-9A-Za-z]{0,1} {0,1}[0-9][A-Za-z]{2}/', >>> 'client') ))); >>> >>> Now the required field is not marked with an asteriks and no >>> validation appears to be executed, client or server side. >> >> This works as expected: >> >> $form->addGroupRule('postcodeGroup', 'Postcode is required', >> 'required', null, >> 1, 'client'); > > Thanks, that gets the asteriks back, but I can't find a happy balance. Now > my additional regular expression rule causes the PHP warning: > > Warning: preg_match() expects parameter 2 to be string, array given in > /usr/share/pear/HTML/QuickForm/Rule/Regex.php on line 61 > > The rule is defined: > > $this->addGroupRule('postcodeGroup', array('postcode' => array( > array($this->language['L_InvalidPostcode'], 'regex', > '/[a-zA-Z]{1,2}[0-9][0-9A-Za-z]{0,1} {0,1}[0-9][A-Za-z]{2}/', 'client') > ))); > > I've also tried defining the rule as you did with the 'required' rule above, > > $form->addGroupRule('postcodeGroup', 'Postcode is required', > 'regex', > '/[a-zA-Z]{1,2}[0-9][0-9A-Za-z]{0,1} {0,1}[0-9][A-Za-z]{2}/', 1, 'client'); > > Both ways cause the same PHP warning and fail to let me proceed when > submitting the form. > I have to try and get this nailed before the afternoon today as work is > dropping on my head too fast :-( Can anybody with group rule validation > experience suggest/help? Please. You don't get the asterisk because your group is not required, only one element in the group is required: 'postcode'. The static element is not required and as such, the group as a whole is not marked required. This works: $form->addRule('postcodeGroup', 'this is required', 'required', null, 'client'); $rule[0][0] = array('bad postcode', 'regex', '/[a-zA-Z]{1,2}[0-9][0-9A-Za-z]{0,1} {0,1}[0-9][A-Za-z]{2}/', 'client'); $form->addGroupRule('postcodeGroup', $rule); But by doing so, you are trying to cheat QuickForm because a static element can't be required. The recommended way would be to improve your template to display help (your static element) as a second label. You can use an array for your element label. Have a look at the examples to see how your template should look like. There is also another way, it would be to use a specific template for your group showing its label. If the label is shown and the required block is present in your template, then the label will have the asterisk. Again, study the examples, the block is called qf_table_group or something like that in ITX examples. Bertrand Mansion Mamasam

« previous php.pear.general (#8807) next »