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

Bug #479 addRule(array(element1, element2)) does not run if element1==''
Submitted: 2003-12-22 15:43 UTC
From: jharlap at bic dot mni dot mcgill dot ca Assigned:
Status: Bogus Package: HTML_QuickForm
PHP Version: Irrelevant OS: irrelevant
Roadmaps: (Not assigned)    
Subscription  
Comments Add Comment Add patch


Anyone can comment on a bug. Have a simpler test case? Does it work for you on a different platform? Let us know! Just going to say 'Me too!'? Don't clutter the database with that please !
Your email address:
MUST BE VALID
Solve the problem : 2 + 38 = ?

 
 [2003-12-22 15:43 UTC] jharlap at bic dot mni dot mcgill dot ca
Description: ------------ When using a compare-style rule, which accepts an array of elements rather than a single element, the validation method is never called if the value of the first of the elements in the array is an empty string. Note that this is not a bug in the compare rule, but rather a shortcoming in QuickForm.php:validate(), as the line 1260 (if ((!isset($submitValue) || $submitValue == '')") filters out empty values. Although the code does override this if $this->isElementRequired($target), there is no way for a user to define an element as required other than by addRule(..., 'required'...);, which is not acceptable for some rules (such as a requiredIf rule). Reproduce code: --------------- $form->addElement('text', 'ele1', 'first element'); $form->addElement('text', 'ele2', 'second element'); $form->addRule(array('ele1', 'ele2'), 'first and second elements must match', 'compare'); $form->validate(); Expected result: ---------------- If you leave the first element field empty (ele1) and type anything into the second field (ele2), then even though the fields do not match, validate() will not catch the error, as it never calls the rule. To prove that the rule is never called, create a custom rule with a validate method as follows: function validate($values, $format) { print_r(func_get_args()); return true;}

Comments

 [2003-12-22 18:30 UTC] jharlap at bic dot mni dot mcgill dot ca
just correcting my email address
 [2003-12-22 19:48 UTC] avb
This is not a bug, the behaviour is documented[1]: On empty elements validation: If the element is empty, no validation rules other than required are checked for it. This means that empty element can be invalid only when it is required. While this not may be quite intuitive for compare rule, it is consistent with the other available rules. Use 'required' rule if you want to make the element required. And I don't know what is a requiredIf rule. [1] http://pear.php.net/manual/en/package.html.html-quickform.intro-validation.php
 [2003-12-22 20:24 UTC] jharlap at bic dot mni dot mcgill dot ca
I'm afraid I must not have been clear. RequiredIf is a rule I am developing for my application, but is just an example of what a user could create in the form of a custom "required" rule. For example: Q3a. Have you had heart surgery? (yes or no) Q3b. If yes, when? (date) In this example, Q3b would be required if Q3a is equal to 'yes'. It is the kind of question-pair that occurs a little too frequently in questionnaires... Unfortunately, the setup of a rule only being validated if the rule is the "required" rule or the element is not empty means that users of the package cannot create their own customized "required" rules, as I've done. Two solutions that would seem to work would be: 1) Add a setting version of the isElementRequired() method, such that one could say setElementRequired($element) (thus forcing rules to be validated even if the value is empty). This would work, but seems a little clumsy. 2) Add a new "required" option to registerRule(), such that instead of "if ($type == 'required' || $type == 'uploadedfile')" on line 865, you would have if(in_array($type, $this->_requiredTypes)), where $this->_requiredTypes would be an array of required rule types, defaulting to array('required', 'uploadedfile') and added to by registerRule(). This option seems very elegant, and reduced the number of hardcoded checks you have in HTML_QuickForm.