EllisLab text mark
Advanced Search
     
Date validation on three form fields
Posted: 02 June 2010 03:33 AM
Joined: 2010-02-13
20 posts

I am stumped as to how I should implement validation for multiple form fields when validation relies on all of the fields. I want it to integrate naturally with CI’s validation so I don’t want to do this after the fact and return a custom message or something like that.

My date forms look like [___]/[___]/[_____] which would be mm/dd/yyyy. My question is how would I setup validation on the days so that for instance, in February, only 1-28 is valid when I can’t see what the month input is before the validation takes place?
Hopefully that makes sense.

Also as a small side question, if you want to add custom validation but want to store your validation in the form_validation config, do you still put the validation function in the controller or do you extend the form_validation class?

 
Posted: 02 June 2010 05:54 AM   [ # 1 ]   [ Rating: 0 ]
Avatar
Joined: 2006-03-25
1027 posts

For checking if the given date is valid have a look here:
http://php.net/manual/en/function.checkdate.php

There are a couple of functions that might help you. I would prefer to
set up dropdown lists to prevent user to fake the date completely.

Also as a small side question, if you want to add custom validation but want to store your validation in the form_validation config, do you still put the validation function in the controller or do you extend the form_validation class?

You can place you validation (callback) function in your controller and define
the rule as needed. Read the user guide / section form validation / callback:
http://ellislab.com/codeigniter/user-guide/libraries/form_validation.html#callbacks

Hope that helps you

 
Posted: 02 June 2010 04:41 PM   [ # 2 ]   [ Rating: 0 ]
Joined: 2010-02-13
20 posts

Thanks for the reply. I looked into the checkdate previously but I am not sure if this is what I am looking for. I think I might have answered my own question by finding another post (http://ellislab.com/forums/viewthread/155128/) on a somewhat related topic. The function I found would work something like:

function valid_date()
    
{
        
if (!checkdate($this->input->post('month'), $this->input->post('day'), $this->input->post('year')))
        
{
            $this
->form_validation->set_message('valid_date''The %s field is invalid.');
            return 
FALSE;
        
}
    } 

I was unaware that you could use data from multiple form fields in a single validation function.

Your idea of having dropdowns is something I considered but from a usability standpoint, I personally prefer form boxes with javascript that automatically moves you to the next box over using dropdowns. It would make life easier though =)

Thanks also for clearing up whether or not you could put custom callbacks in the config file for me.

 
Posted: 03 June 2010 04:59 AM   [ # 3 ]   [ Rating: 0 ]
Avatar
Joined: 2006-03-25
1027 posts

If you use js than a datepicker could be quite nice. I implement them
in my backends for clients managing their news and stuff. There is a
jquery plugin working nice and you can customize it to your needs.

You can find a demo at the bottom of this page:
http://docs.jquery.com/UI/Datepicker

 
Posted: 03 June 2010 05:40 PM   [ # 4 ]   [ Rating: 0 ]
Avatar
Joined: 2009-02-19
4329 posts
LuckyFella73 - 03 June 2010 08:59 AM

If you use js than a datepicker could be quite nice. I implement them
in my backends for clients managing their news and stuff. There is a
jquery plugin working nice and you can customize it to your needs.

You can find a demo at the bottom of this page:
http://docs.jquery.com/UI/Datepicker

Datepickers sure help, but you should always validate nonetheless.  Most datepickers still allow manual entry into the form field, so human error is still very possible.

 Signature 
 
Posted: 04 June 2010 04:38 AM   [ # 5 ]   [ Rating: 0 ]
Avatar
Joined: 2006-03-25
1027 posts

Datepickers sure help, but you should always validate nonetheless.

You are absolutely right! My hint was just ment to be helpfull for usability
reasons. Validation should allways be made on the server side! Sometimes it’s good to
check required fields with js to prevent users to have to reselect files again and
again after submitting an uncomplete form.

 
Posted: 04 June 2010 04:06 PM   [ # 6 ]   [ Rating: 0 ]
Joined: 2010-02-13
20 posts

That was exactly what I had planned. I wanted to have three form fields that would auto tab to the next or previous form fields and then a date picker that could fill in the forms as well. My previous solution above doesn’t seem to work with the way that validation is setup in CI. For some reason the validation messages are not displaying correctly and whatever field I put the validation on, it won’t remember the inputted value using set_value.