PEAR Forum :: PHP Extension and Application Repository

PEAR Forum Forum Index
 FAQFAQ   SearchSearch   MemberlistMemberlist   RegisterRegister   ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 
HTML_Quickform: date validation

 
Post new topic   Reply to topic    PEAR Forum Forum Index -> HTML
View previous topic :: View next topic  
Author Message
Tobik



Joined: 18 Jan 2007
Posts: 2

PostPosted: Thu Jan 18, 2007 1:54 am    Post subject: HTML_Quickform: date validation Reply with quote

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
View user's profile Send private message Send e-mail
mark



Joined: 07 Jan 2007
Posts: 1053

PostPosted: Thu Jan 18, 2007 9:56 am    Post subject: Reply with quote

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
View user's profile Send private message
Tobik



Joined: 18 Jan 2007
Posts: 2

PostPosted: Thu Jan 18, 2007 11:03 pm    Post subject: Reply with quote

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 Smile
IMHO, it should be a self-validated object.

Best regards,

Tobik.
Back to top
View user's profile Send private message Send e-mail
mark



Joined: 07 Jan 2007
Posts: 1053

PostPosted: Thu Jan 18, 2007 11:07 pm    Post subject: Reply with quote

Tobik wrote:
Thanks a lot for your help. It's not a big deal, I guess Smile


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
View user's profile Send private message
marxav



Joined: 03 May 2007
Posts: 8

PostPosted: Thu May 03, 2007 8:18 pm    Post subject: Reply with quote

Hi
I am now to PEAR and to this forum Wink

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
View user's profile Send private message
mark



Joined: 07 Jan 2007
Posts: 1053

PostPosted: Thu May 03, 2007 8:41 pm    Post subject: Reply with quote

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
View user's profile Send private message
marxav



Joined: 03 May 2007
Posts: 8

PostPosted: Thu May 03, 2007 9:22 pm    Post subject: Reply with quote

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
View user's profile Send private message
mark



Joined: 07 Jan 2007
Posts: 1053

PostPosted: Thu May 03, 2007 9:28 pm    Post subject: Reply with quote

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
View user's profile Send private message
marxav



Joined: 03 May 2007
Posts: 8

PostPosted: Thu May 03, 2007 10:42 pm    Post subject: Reply with quote

Wow I went the YourClassName route. This is outstanding!!

Thanks.
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    PEAR Forum Forum Index -> HTML All times are GMT + 2 Hours
Page 1 of 1

 
Jump to:  
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



PEAR Forum topic RSS feed 
Powered by phpBB © 2001, 2005 phpBB Group

Provided by Ministry of Web developement

'Actiemonitor' online projectmanagement software