Re: quickform addGroupRule question

From: Date: Sun, 02 Feb 2003 16:15:42 +0000
Subject: Re: quickform addGroupRule question
References: 1  Groups: php.pear.general 
Request: Send a blank email to pear-general+get-3556@lists.php.net to get a copy of this message
<culley@ml1.net> wrote : > I would like to use quickform and date/calc.php to do som basic date > verification. The following code doesn't work. Is there another way to > get this to work? > > ====================================== > <?php > > require_once ("HTML/QuickForm.php"); > require_once("Date/Calc.php"); > > $form =& new HTML_QuickForm(); > $test_date['month'] = &HTML_QuickForm::createElement('select', > 'month', > '', array('0' => '-month-', '1' => > 'January', '2' => 'February', '3' => > 'March', '4' => 'April', '5' => 'May', > '6' => 'June', '7' => 'July', '8' > => 'August', '9' => 'September', '10' => > 'October', '11' => 'November', > '12' => 'December')); > $test_date['day'] = &HTML_QuickForm::createElement('text', > 'day', '', > 'size=2 maxlength=2'); > $test_date['year'] = &HTML_QuickForm::createElement('text', > 'year', '', > 'size=4 maxlength=4'); > $form->addElementGroup($test_date, 'Next Session (month, day, year):', > 'test_date'); > > $form->addElement('submit', 'save_button', 'Save'); > $form->applyFilter('__ALL__', 'trim'); > $form->registerRule("validDateFormat", "function", > "validDateFormat"); > $form->addGroupRule('test_date', 'Values must be numeric', > 'numeric'); > $form->addGroupRule('test_date', 'Invalid date', > 'validDateFormat'); > > if ($form->validate()) { > echo "done"; > exit; > } > > function validDateFormat($element, $value, $defaultValue) > { > extract($_POST["test_date"], EXTR_OVERWRITE); > return Date_Calc::isValidDate($day, $month, $year); > > } > > $form->display(); > > ?> Hi Culley, In CVS, I have added this possibility. Look at QuickForm.php v1.56 Here is the code you should use if you want such a feature: <?php require_once ("HTML/QuickForm.php"); require_once("Date/Calc.php"); $form =& new HTML_QuickForm(); $test_date['month'] = &HTML_QuickForm::createElement('select', 'month', '', array('0' => '-month-', '1' => 'January', '2' => 'February', '3' => 'March', '4' => 'April', '5' => 'May', '6' => 'June', '7' => 'July', '8' => 'August', '9' => 'September', '10' => 'October', '11' => 'November', '12' => 'December')); $test_date['day'] = &HTML_QuickForm::createElement('text', 'day', '', 'size=2 maxlength=2'); $test_date['year'] = &HTML_QuickForm::createElement('text', 'year', '', 'size=4 maxlength=4'); $form->addElementGroup($test_date, 'Next Session (month, day, year):', 'test_date'); $form->addElement('submit', 'save_button', 'Save'); $form->applyFilter('__ALL__', 'trim'); $form->registerRule("validDateFormat", "function", "validDateFormat"); $form->addGroupRule('test_date', 'Values must be numeric', 'numeric'); $form->addRule('test_date', 'Invalid date', 'validDateFormat'); if ($form->validate() === true) { echo "done"; exit; } function validDateFormat($element, $value, $defaultValue) { return Date_Calc::isValidDate($value['day'], $value['month'], $value['year']); } $form->display(); ?> The main differences are: 1. Use addRule instead of addGroupRule. addRule will validate your group as a whole, as if it was a single element, while addGroupRule will validate elements of a group taken individually, one after the other. 2. Use $value to get the values of your group array in your validation function. Please tell me if this create other problems I'd have overlooked. Bertrand Mansion Mamasam

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