Re: QuickForm group validation rules
| From: | Andrew Zahra | Date: | Fri, 19 Aug 2005 08:03:45 +0000 |
| Subject: | Re: QuickForm group validation rules | ||
| References: | 1 2 | Groups: | php.pear.general |
| Request: | Send a blank email to pear-general+get-20479@lists.php.net to get a copy of this message | ||
Alexey Borzov wrote:
I've seen all types of weird behaviour and noticed a following tendency: if a person claims a bug in QuickForm without showing the code to trigger it then a) he is too ashamed to show his code due to its low quality; b) the bug is actually within his code and extremely easy to spot.Or c) its a lot of work to extract a good example and he was hoping someone might have had the same problem :) Following is a snippet of code using the rule followed by the actual validation rule. It is validating a date as three fields of text as I want them to be able to leave it blank when optional. I couldn't see how i could do that with the date element. I hope its clear... Here is the segment of code that is using the rule:
$date[] = &$this->createElement('text', 'day', null, array('size' => 2));
$date[] = &$this->createElement('text', 'month', null, array('size' => 2));
$date[] = &$this->createElement('text', 'year', null, array('size' => 4));
$this->addGroup($date, 'wedDate', 'Wedding Date (D/M/Y)');
// only mandatory if married
if ($regType == 'cm')
{
$wedrq = 'Please provide your wedding date';
$wedinv = 'Please enter a valid date';
$this->addRule('wedDate', $wedrq, 'required', null, CV);
}
$this->addGroupRule('wedDate', array( 'day' => array(array('Please enter a valid day', 'numeric', null)), 'month' => array(array('Please enter a valid month', 'numeric', null)),
'year' => array(array('Please enter a 4 digit year', 'minlength', 4),
array('Please enter a valid year', 'numeric', null)),
));
$this->registerRule('groupdate', null, 'ValidateDateGroupRule');
$this->addRule('wedDate', $wedinv, 'groupdate', null, CV);
---snip---
And here is the actual rule:
class ValidateDateGroupRule extends HTML_QuickForm_Rule
{
function validate($value, $options)
{
//echo 'validatedate<br/>';
// must have 3 date components
if (count($value) != 3){ return false; } // check all numeric foreach ($value as $dc) { if (! is_numeric($dc)) { return false; } } // get date components reset($value); $day = current($value); $month = next($value); $year = next($value); // check if valid return checkdate($month,$day,$year);
} // end func validate
function getValidationScript($options = null)
{
return array('', "{jsVar} == ''");
} // end func getValidationScript
} // end class