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

Bug #2517 applyFilter activates the validation
Submitted: 2004-10-13 08:23 UTC
From: max at tvattomaten dot se Assigned: avb
Status: Closed Package: HTML_QuickForm
PHP Version: 4.3.8 OS: Win XP
Roadmaps: (Not assigned)    
Subscription  


 [2004-10-13 08:23 UTC] max at tvattomaten dot se
Description: ------------ When adding a filter for my form I've noticed that the applyFilter function filters automatically the values and puts them into the submitted values array: foreach ($element as $elName) { $value = $this->getSubmitValue($elName); if (null !== $value) { if (false === strpos($elName, '[')) { $this->_submitValues[$elName] = $this->_recursiveFilter($filter, $value); } else { $idx = "['" . str_replace(array(']', '['), array('', "']['"), $elName) . "']"; eval("\$this->_submitValues{$idx} = \$this->_recursiveFilter(\$filter, \$value);"); } } } I find this very annoying since if I use any default variables they seem to end up in the _submitValues array resulting in that when I do a validate() it won't detect properly that the form hasn't been submitted. My suggestion is that either submitValues is checked for bugs when using grouped elements, applyFilter is changed or an isFormSubmitted() action is added. Reproduce code: --------------- My code goes as: /** * Function for checkin a date that has been inputted * and changing it to a YYYY-MM-DD format. If an invalid * date was supplied then the function returns false. * * @access public * @return string If false it will return boolean FALSE **/ function CheckAndReformatDate($DateString){ $DateString = trim(strval($DateString)); // Correct date ex. 2004-01-15 if (eregi("^20[0-9]{2}-[0-1]{1}[0-9]{1}-[0-3]{1}[0-9]{1}$", $DateString)){ //Nothing to do // Date formatted as ex. 04-01-15 }elseif (eregi("^[0-9]{2}-[0-1]{1}[0-9]{1}-[0-3]{1}[0-9]{1}$", $DateString)){ $DateString = "20" . $DateString; // Date formatted as ex. 040115 }elseif (eregi("^[0-9]{2}[0-1]{1}[0-9]{1}[0-3]{1}[0-9]{1}$", $DateString)){ $DateString = "20" . substr($DateString, 0, 2) . "-" . substr($DateString, 2, 2) . "-" . substr($DateString, 4, 2); // Date formatted as ex. 20040115 }elseif (eregi("^20[0-9]{2}[0-1]{1}[0-9]{1}[0-3]{1}[0-9]{1}$", $DateString)){ $DateString = substr($DateString, 0, 4) . "-" . substr($DateString, 4, 2) . "-" . substr($DateString, 6, 2); // Date formatted as ex. 03/01 04 }elseif (eregi("^[0-3]{1}[0-9]{1}\/[0-1]{1}[0-9]{1}[[:space:]][0-9]{2}$", $DateString)){ $DateString = "20" . substr($DateString, 6, 2) . "-" . substr($DateString, 3, 2) . "-" . substr($DateString, 0, 2); // Date formatted as ex. 3/01 03 }elseif (eregi("^[1-9]{1}\/[0-1]{1}[0-9]{1}[[:space:]][0-9]{2}$", $DateString)){ $DateString = "20" . substr($DateString, 6, 2) . "-" . substr($DateString, 3, 2) . "-0" . substr($DateString, 0, 2); // Date formatted as ex. 03/11 }elseif (eregi("^[0-3]{1}[0-9]{1}\/[0-1]{1}[0-9]{1}$", $DateString)){ $TodayDate = getdate(); $DateString = $TodayDate["year"] . "-" . substr($DateString, 3, 2) . "-" . substr($DateString, 0, 2); // Date formatted as ex. 03/8 }elseif (eregi("^[0-3]{1}[0-9]{1}\/[0-9]{1}$", $DateString)){ $TodayDate = getdate(); $DateString = $TodayDate["year"] . "-0" . substr($DateString, 3, 1) . "-" . substr($DateString, 0, 2); // Date formatted as ex. 3/11 }elseif (eregi("^[0-9]{1}\/[0-1]{1}[0-9]{1}$", $DateString)){ $TodayDate = getdate(); $DateString = $TodayDate["year"] . "-" . substr($DateString, 2, 2) . "-0" . substr($DateString, 0, 1); // Date formatted as ex. 3/8 }elseif (eregi("^[0-9]{1}\/[0-9]{1}$", $DateString)){ $TodayDate = getdate(); $DateString = $TodayDate["year"] . "-0" . substr($DateString, 2, 1) . "-0" . substr($DateString, 0, 1); // Date formatted as ex. 03 }elseif (eregi("^[0-3]{1}[0-9]{1}$", $DateString)){ $TodayDate = getdate(); if (intval($TodayDate["mon"]) < 10) { $DateString = $TodayDate["year"] . "-0" . intval($TodayDate["mon"]) . "-" . substr($DateString, 0, 2); }else{ $DateString = $TodayDate["year"] . "-" . intval($TodayDate["mon"]) . "-" . substr($DateString, 0, 2); } // Date formatted as ex. 3 }elseif (eregi("^[1-9]{1}$", $DateString)){ $TodayDate = getdate(); if (intval($TodayDate["mon"]) < 10) { $DateString = $TodayDate["year"] . "-0" . intval($TodayDate["mon"]) . "-0" . substr($DateString, 0, 2); }else{ $DateString = $TodayDate["year"] . "-" . intval($TodayDate["mon"]) . "-0" . substr($DateString, 0, 2); } // If the user wants a date x days ahead }elseif (eregi("^\+\s*[1-9]{1}[0-9]*$", $DateString)){ $DateString = date("Y-m-d",strtotime(trim(substr($DateString, 1, strlen($DateString) - 1)) . " days")); }elseif (eregi("^\-\s*[1-9]{1}[0-9]*$", $DateString)){ $DateString = date("Y-m-d",strtotime(trim(substr($DateString, 1, strlen($DateString) - 1)) . " days ago")); }elseif (eregi("igår", $DateString)){ $DateString = date("Y-m-d",strtotime("yesterday")); }elseif (eregi("förrgår", $DateString)){ $DateString = date("Y-m-d",strtotime("2 days ago")); }else{ // No valid date pattern was found return FALSE; } // Check if the date is a valid one. if (checkdate(substr($DateString, 5, 2), substr($DateString, 8, 2), substr($DateString, 0, 4)) == FALSE) { $DateString = FALSE; } return $DateString; } function FilterDate($DateString){ $Date = CheckAndReformatDate($DateString); if (MyCheckDate($Date)) { return $Date; }else{ return $DateString; } } $form =& new HTML_QuickForm('OrderForm', 'POST', '', '_self', 'autocomplete ="off"', true); $form->setRequiredNote('<font color="#FF0000">*</font> Obligatoriska fält.'); $form->setJsWarnings('Följande fält är felaktiga:', 'Tacksom om du kan rätta till misstagen.'); $date['startDate'] =& HTML_QuickForm::createElement('text', 'startDate', 'Första datum för beställning', 'size=10 align=right'); $date['endDate'] =& HTML_QuickForm::createElement('text', 'endDate', 'Dista datum för beställning', 'size=10 align=right'); $form->addGroup($date, 'date', '<a name="date">Datum:</a>'); $form->addGroupRule('date', array( 'startDate' => array(array('Ogiltigt första datum angivet.', 'callback', 'ValidateDate'), array('Du måste ange ett första datum.', 'required')), 'endDate' => array(array('Ogiltigt andra datum angivet.', 'callback', 'ValidateDate')))); $form->addElement('submit', '', 'Spara beställning'); $TodayDate = date("Y-m-d",strtotime("now")); $DefaultValues["date"]["startDate"] = $TodayDate; $DefaultValues["date"]["endDate"] = $TodayDate; $form->setDefaults($DefaultValues); $form->addFormRule('ValidateOrder'); $form->applyFilter("date", 'FilterDate'); // Tries to validate the form if ($form->validate()) { die("This is validated"); } print($form->toHTML()); Expected result: ---------------- The form should be outputted without going through the validatoin process. Actual result: -------------- I get that one of the dates are not properly validated. This is absurd since I haven't submitted the form! When removing the applyFilter this issue disappears.

Comments

 [2004-10-13 10:41 UTC] avb
Sorry for knee-jerk reaction, that's definitely not that particular FAQ problem. After I've run the example through debugger it looks like getSubmitValues() may return junk for groups in case the form was not yet submitted, therefore the problem. I'll investigate further and will try to create a smaller test case.
 [2004-10-13 10:56 UTC] avb
A *much* smaller testcase: <?php require_once 'HTML/QuickForm.php'; $form =& new HTML_QuickForm('bug2517', 'POST'); $txt[] =& $form->createElement('text', 'bar'); $txt[] =& $form->createElement('text', 'baz'); $form->addGroup($txt, 'foo', 'Foo:'); $form->addGroupRule('foo', array( 'bar' => array( array('Foobar is required', 'required') ), 'baz' => array( array('Foobaz is required', 'required') ) )); $form->applyFilter('foo', 'trim'); $form->validate(); $form->display(); ?> Looks like getSubmitValue() needs fixing.
 [2004-10-14 13:21 UTC] avb
This bug has been fixed in CVS. In case this was a documentation problem, the fix will show up at the end of next Sunday (CET) on pear.php.net. In case this was a pear.php.net website problem, the change will show up on the website in short time. Thank you for the report, and for helping us make PEAR better.