Re: HTML_QuickForm methods as rules

From: Date: Sun, 02 Feb 2003 17:52:36 +0000
Subject: Re: HTML_QuickForm methods as rules
References: 1  Groups: php.pear.general 
Request: Send a blank email to pear-general+get-3558@lists.php.net to get a copy of this message
<raidel@i-one.at> wrote : > Hello, > > I am just trying out HTML_QuickForm and have a little problem: it seems > to me, that it's only possible to "registerrule" a function thats not in > a class because HTML_QuickForm doesn't know anything about the > Class-Object. The registerrule-function has an parameter calles $data2 > which is described as "Object parent of above function". Either I make > something wrong, or QuickForm doesn't use this parameter at all. I now > inserted the following line in the function "validate", directly below > the "case 'function': > if ($ruleData[2]) { $function_object =& $ruleData[2]; } else { > $function_object =& $this; } > > now I use $function_object instead of $this for the "method_exists" and > for the if-clause which tests the success of the rule: "if > (!$function_object->$ruleData[1]($elementName, $element, $format)) {" > > Now all the internal functions continue to work fine, but it's also > possible to include a method in the calling script with a line like this: > $this->form->registerRule('compare', 'function', 'cmpPass', > $this); > > > I'm looking forward to hear what you think about this small change. > > Michael > > P.S.: Would be nice to be able to include additional arguments as an > array with each addRule-call. The problem is, that there are a lot of > other arguments in the addRule-function already. An additional argument > would make it necessary to put all arguments in the call. Maybe someone > has a better idea... > > P.P.S.: > Has anyone an idea why $function_object =& ($ruleData[2]) ? $ruleData[2] > : $this; doesn't work with =&, only with =? No references possible with > the ternary operator? Not so important, was just wondering about this ;-) > Hi Michael, It is now possible to use a method from a class or from an object to validate your form elements. Here is an example with the Validate class from PEAR: <?php require_once ("HTML/QuickForm.php"); require_once ("Validate.php"); $form =& new HTML_QuickForm(); $form->addElement('text', 'email', 'Your mail:'); $form->addElement('submit', 'save_button', 'Save'); $form->registerRule('Validate_email', 'function', 'email', 'Validate'); $form->addRule('url', 'not an email', 'Validate_email', true); if ($form->validate() == true) { echo 'OK<br />'; } $form->display(); ?> The only concern is about the $format parameter. Some other methods in Validate are not designed to work with QuickForm because they accept too much parameters. When designing your validation methods, remember to use only 2 parameters: One for the value(s), one for the format(s) or extra parameters. Both can be arrays. In my Validate_email rule, I set the format to true which means I also want the validation method to check the domain. IMO, it would be great if all methods from Validate.php could be rewritten to accept only 2 parameters... ;) Bertrand Mansion Mamasam

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