 |
|
|
| View previous topic :: View next topic |
| Author |
Message |
Tobik
Joined: 18 Jan 2007 Posts: 2
|
Posted: Thu Jan 18, 2007 1:54 am Post subject: HTML_Quickform: date validation |
|
|
Hi
Here is a simple code from one of tutorial:
<?php
require_once "HTML/QuickForm.php";
$form = new HTML_QuickForm('frmTest', 'get');
$options = array(
'language' => 'en',
'format' => 'dMYHi',
'minYear' => 2001,
'maxYear' => 2005
);
$form->addElement('date', 'mydate', 'Choose date', $options);
$form->addElement('submit', 'btnSubmit', 'Submit');
if ($form->validate()) {
// Form is validated, then processes the data
$form->freeze();
$form->process('process_data', false);
}
else {
$form->display();
}
function process_data ($values) {
echo "<pre>";
var_dump($values);
echo "</pre>";
}
?>
It is possible to select an invalid date, like 31 February. How to create a rule, which validate it?
Regards,
Tobik. |
|
| Back to top |
|
 |
mark

Joined: 07 Jan 2007 Posts: 1004
|
Posted: Thu Jan 18, 2007 9:56 am Post subject: |
|
|
| You need to add a rule with a callback function (cp. the manual examples for more details). This callback can then call PHP's checkdate() function to validate the submitted date. The values of the date element are available as an array with the keys as defined in your script (in your case: 'd', 'm', 'Y') inside the callback function. |
|
| Back to top |
|
 |
Tobik
Joined: 18 Jan 2007 Posts: 2
|
Posted: Thu Jan 18, 2007 11:03 pm Post subject: |
|
|
| mark wrote: | | You need to add a rule with a callback function (cp. the manual examples for more details). This callback can then call PHP's checkdate() function to validate the submitted date. The values of the date element are available as an array with the keys as defined in your script (in your case: 'd', 'm', 'Y') inside the callback function. |
Thanks a lot for your help. It's not a big deal, I guess
IMHO, it should be a self-validated object.
Best regards,
Tobik. |
|
| Back to top |
|
 |
mark

Joined: 07 Jan 2007 Posts: 1004
|
Posted: Thu Jan 18, 2007 11:07 pm Post subject: |
|
|
| Tobik wrote: | Thanks a lot for your help. It's not a big deal, I guess  |
It isn't, just a few lines of code.
| Tobik wrote: | | IMHO, it should be a self-validated object. |
What are talking / thinking of here? |
|
| Back to top |
|
 |
marxav
Joined: 03 May 2007 Posts: 8
|
Posted: Thu May 03, 2007 8:18 pm Post subject: |
|
|
Hi
I am now to PEAR and to this forum
I added a validate function in the QuickForm rule directory like so :
| Code: | function validate($value, $options = null)
{
if (!checkdate(date('m',$value),date('d',$value),date('Y',$value))) {
return false;
}
return true;
} // end func validate
|
And I added a validation registration rule in QuickForm.php lke so
| Code: | 'date' => array('html_quickform_rule_date', 'HTML/QuickForm/Rule/Date.php')
|
I don't know if this makes sense, but it does the trick and I use it like so :
| Code: | $form->addRule('dateenrolement', 'N\'est pas une date', 'date');
|
|
|
| Back to top |
|
 |
mark

Joined: 07 Jan 2007 Posts: 1004
|
Posted: Thu May 03, 2007 8:41 pm Post subject: |
|
|
| Instead of modifying QuickForm.php (with the risk that your change will be overwritten on the next update), you should use $form->registerRule(). |
|
| Back to top |
|
 |
marxav
Joined: 03 May 2007 Posts: 8
|
Posted: Thu May 03, 2007 9:22 pm Post subject: |
|
|
Hum interesting, but I am not sure how to use this.
$form->registerRule('date','callback','isDate'...
I get stuck here. Am I supposed to put callback, and where does my function isDate goes? I read this http://pear.php.net/manual/en/package.html.html-quickform.html-quickform.registerrule.php but I am still confused.
PS Yes I agree, modifying QuckForm is not a good thing, but by notifying of this, I was hoping it woud get implemented in future release of QuickForm. I think we all agree that checking for date is very common task. |
|
| Back to top |
|
 |
mark

Joined: 07 Jan 2007 Posts: 1004
|
Posted: Thu May 03, 2007 9:28 pm Post subject: |
|
|
Please have a look at http://pear.php.net/manual/en/package.html.html-quickform.intro-validation.php, Example 43-1, and the text following the example.
In your case it would be something like
| Code: | | $form->registerRule('date', 'callback', 'validate', 'YourClassName'); |
and you would need to include the file containing the class yourself. Or simpler, just put the validation function with a name like checkDate() somewhere into your code, and use just | Code: | | $form->registerRule('date', 'callback', 'checkDate'); |
|
|
| Back to top |
|
 |
marxav
Joined: 03 May 2007 Posts: 8
|
Posted: Thu May 03, 2007 10:42 pm Post subject: |
|
|
Wow I went the YourClassName route. This is outstanding!!
Thanks. |
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|