Home » HTML » HTML_QuickForm » Bug #398
user function for form rule cannot accept user arguments
Details
| Submitted | 2003-12-10 15:05 UTC |
|---|---|
| From | jharlap at bic dot mni dot mcgill dot ca |
| Status | Wont fix |
| Package | HTML_QuickForm |
| PHP Version | 4.3.3 |
| OS | Irrelevant |
| Roadmaps | (Not assigned) |
Comments
[2003-12-10 15:05 UTC] jharlap at bic dot mni dot mcgill dot ca
Description:
------------
The addFormRule and validate methods of QuickForm do not provide the option of passing arguments to the form rule callback function other than the form values and uploaded files. A simple patch (perhaps not ideal - but the concept is good IMO) which remedies this shortcoming is available at http://www.bic.mni.mcgill.ca/~jharlap/php/quickform/accept_form_rule_arguments.patch
I'm sorry if this isn't the right place for a patch...
Jon
Reproduce code:
---------------
// given a function with prototype myCallbackFunction($values, $files, $myArg)
$form->addFormRule(array('myCallbackFunction', 'arg'=>'an argument passed to myCallbackFunction'));
// or
// given a class myClass with a method myRuleMethod following the same prototype as myCallbackFunction above
$callbackArgs = array(1,2,3);
$form->addFormRule(array('myClass', 'myRuleMethod', 'arg'=>$callbackArgs));
[2003-12-10 20:16 UTC] jharlap at bic dot mni dot mcgill dot ca
I'm sorry - the patch was sloppy in that I overlooked the if(is_callable(...)) bit in addFormRule (and that it was not elegantly written). The issue of running validate() more than once is null, however, as the foreach($this->_formRules...) loop surrounding call_user_func copies each rule by value rather than by reference, so $this->_formRules never gets edited by the unset() I had added.
I disagree with the philosophical approach to addFormRule, in that it seems to be inconsistent with addRule. addRule accepts an optional argument for callback functions, $format, which allows for flexible reusable rules such as the regex rule. Having to instantiate a new object for each instance of the same form rule just to be able to set different properties on that form rule object is IMO contradictory to the apparant design of addRule.
I respect that this patch is not ideal for the PEAR package. I will (now that I've had some coffee) write a more correct patch and add a link to it here for your consideration, in the hope that you will agree with my philosophical perspective above and accept the patch for QuickForm.
[2003-12-10 20:38 UTC] jharlap at bic dot mni dot mcgill dot ca
Here's the new patch - it is significantly simpler and more elegant and should make addFormRule behave much more like addRule. The one required user change would be that with this patch any form rule functions which want to use the new argument would have to have prototype "function myFunction($submitValues, $submitFiles, $myArgument)".
http://www.bic.mni.mcgill.ca/~jharlap/php/quickform/accept_form_rule_arguments.patch
I look forward to your comments.
[2003-12-11 12:29 UTC] jharlap at bic dot mni dot mcgill dot ca
Interesting perspective. The only issue is that in the manual you suggest that in order to create a validation rule that accesses more than one form element value, you need to make a form rule. It seems that at some later date the compare rule was added, demonstrating a way to access multiple form fields at once, but in a non-group friendly way.
I have been trying to create a requiredIf rule, such that one element is required if another element satisfies some criteria (such as "If yes to #3, describe:"). The compare approach won't work for us, as we generally use groups in this situation.
Thus, it seems that the only practical and viable approach is a parametrized form rule. I grant that objects could solve the same problem, and given no other option we will use them, but the resultant code becomes MUCH longer and more complex.