Package home | Report new bug | New search | Development Roadmap Status: Open | Feedback | All | Closed Since Version 3.2.16

Bug #1810 regex addRule() can't validate select data
Submitted: 2004-07-06 19:36 UTC
From: danielc Assigned: avb
Status: Closed Package: HTML_QuickForm
PHP Version: Irrelevant OS: Irrelevant
Roadmaps: (Not assigned)    

 [2004-07-06 19:36 UTC] danielc
Description: ------------ When validating array input against regex rules HTML_QuickForm should loop through the array and run each of the array's value through the regex. Alternatively, one could create a new rule $type called something like "in" which takes an array for the $format. Reproduce code: --------------- <?php require_once 'HTML/QuickForm.php'; $form = new HTML_QuickForm('text', 'post', ''); $arr = array( 'one' => 'uno', 'two' => 'dos', ); $form->addElement('select', 'n', '', $arr, array('size' => count($arr), 'multiple' => 'multiple')); $form->addElement('submit', 'submit', 'Submit'); $form->addRule('n', 'n contains invalid value.', 'regex', '/' . implode('|', array_keys($arr)) . '/'); if (isset($_POST['submit'])) { if ($form->validate()) { echo 'Thanks.'; } } $form->display(); ?> Expected result: ---------------- Thanks. Actual result: -------------- Warning: preg_match() expects parameter 2 to be string, array given in c:\Program Files\pear\pear\HTML\QuickForm\Rule\Regex.php on line 61


 [2004-10-05 19:46 UTC] wormus
This one has bit me a few times with hierselects. I've patched it to meet my needs(it's a bit hackish). You can find it at It has worked in everything I've thrown at it, and worked in the above scenario, but has not been stress tested.
 [2004-10-10 11:16 UTC] avb
You can use addGroupRule() to validate values one by one instead of validating the whole array (this is the same behaviour as with groups). The following code will work with your example: $form->addGroupRule('n', 'n contains invalid value.', 'regex', '/' . implode('|', array_keys($arr)) . '/', count($form->getSubmitValue('n')) ); I also patched QuickForm a bit so that addGroupRule() will work without errors if applied to a <select multiple> element (especially with 'required' type rules). Please note that you *should* correctly set $howmany parameter.