Welcome Guest, Not a member yet? Register   Sign In
Date validation on three form fields
#1

[eluser]MT206[/eluser]
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?
#2

[eluser]LuckyFella73[/eluser]
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.

Quote: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-gui...#callbacks

Hope that helps you
#3

[eluser]MT206[/eluser]
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:

Code:
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.
#4

[eluser]LuckyFella73[/eluser]
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
#5

[eluser]CroNiX[/eluser]
[quote author="LuckyFella73" date="1275569965"]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[/quote]
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.
#6

[eluser]LuckyFella73[/eluser]
Quote: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.
#7

[eluser]MT206[/eluser]
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.




Theme © iAndrew 2016 - Forum software by © MyBB