QF validate()ing empty fields [WAS] Quickform Element Conditional Rule
| From: | Aaron Wormus | Date: | Fri, 21 Jan 2005 08:52:29 +0000 |
| Subject: | QF validate()ing empty fields [WAS] Quickform Element Conditional Rule | ||
| References: | 1 2 3 4 | Groups: | php.pear.general |
| Request: | Send a blank email to pear-general+get-17070@lists.php.net to get a copy of this message | ||
Justin Patrin wrote:
I think you'd have to implement your own rule class and override getValidationScript. OK, I've done that, but now it seems that Quickform does not send empty fields through the validate() method. Following is my custom rule:require_once('HTML/QuickForm/Rule.php'); class requiredIf extends HTML_QuickForm_Rule { function validate($value, $options = null) {
return false;} function getValidationScript($options = null) {
$jstring = array();
foreach ($options as $key => $value){
$jstring[] = "frm.elements['$key'].value == '$value'";
}
$jsstring = join(' && ', $jstring);
return array('', "$jsstring && {jsVar} == ''");
}
}
This is invoked like this:
$form->registerRule('requiredif', null, 'requiredIf', 'requiredif.rule.php');
$form->addRule('ccnumber',
'You must fill in Your Creditcard Number',
'requiredif',
array('payment_type' => 'CC'),
"client");
This works fine with the javascript, but because it seems that the validate() method is not called unless the field contains a value, the server side checking is useless.
The only way I see of getting around this is to create another serverside rule with addformrule, but I would perfer to have it all in the same rule.
Ideas?
Aaron