DATETIME validation in CodeIgniter or how to create your own validation rules with callbacks and/or extending the form validation library

Created at: May 18, 2015; Last update: May 18, 2015

I found myself wanting to validate an input field that will be used when inserting the POST values in a table. That input field would have to be a DATETIME type of value. That is to say that if someone will fill the value of an input field, let’s name the input field “published_at”, that value would have to have a DATETIME format: 2015-05-18 15:10:29.

So how would we go about doing this?

Firstly, we would create the input field (this tutorial is not about how to create a form. For this you can at any time study the manual):

After this we go to our form validation rules and create a rule for this input (let me mention that this tutorial is also not about how to do form validation in CodeIgniter…):

Now we will only have to create the callback function (actually a method inside our controller). For starters let’s create the basic setup. As the manual states, we will create a function named just like the rule, without the “callback_” part:

Now, if we fill the input field with “test” value, it should throw us the error.

Cool. So we’re on the right track. Now, how would we go about validating the DATETIME:

First we will validate the date:

Now we will have to verify if the right time was inserted:

If no return was done until this point, we simply return TRUE:

And that’s it. Now let’s see the function again:

But…

But what if we know we will need this form validation rule in more than one controller? For this we can create a MY_Form_validation.php file in the application/libraries directory. We do this to extend the core functionality of the CodeIgniter framework without messing with the system files.

Now let’s just create a class named MY_Form_validation that will extend CI_Form_validation and then copy the method from our controller into our new library. We will rename the method datetime() as this name is actually more representative for the rule:

 

After this we go to where we’ve set the form validation rules, and change the rule so that it will be using the new method:

If we try to do a DATETIME validation we will get some slew of errors. That is because we try to call methods of a form_validation object that doesn’t exist ($this->form_validation->set_message()).

So let’s return to our MY_Form_validation and make the changes accordingly:

All looks OK, but what if we want to have fast access to the errors so that we can change what the messages say? Let’s just start by moving all the messages inside an array that we will define in the constructor, and call those messages inside our method:

 

…And still…

…And still… it just doesn’t feel right? What about having messages for another language? You could of course make custom messages for the rule, but that would get burdensome on the long run.

Now, if we look at the Form_validation library inside the system/libraries directory, we can see that the messages are called automatically from the form_validation language file (form_validation_lang.php) if the result of the validation is false. So why not use this?

So why not have an easier life by creating ourselves a file like that inside the application/language/english directory. If we do this, CodeIgniter will append the values inside that file to the values inside the system’s file. Once we’ve created  application/language/english/form_validation_lang.php we only have to write a line in it:

Now let’s go again to our MY_Form_validation class and change it to see if it takes the message from the language file:

 

Now, if in the input field we put something else than a DATETIME type of value, the form validation library will additionally “use” our MY_Form_validation and look for the validation error message inside the application/language/english/form_validation_lang.php which will be outputted in case of error.

Hope you’ve enjoyed it.

2 thoughts on “DATETIME validation in CodeIgniter or how to create your own validation rules with callbacks and/or extending the form validation library

  1. Federico

    Really intresting tutorial…exactly what I need for my project.
    In my app I need to store the date in the format of yyyy-mm-dd, with no time…and sometimes user could just add the year, whitout month and day…using checkdate function I cannot do that…any hint to tweak to script? Thanks

    Reply
  2. Aurel

    @federico:
    In MY_Form_validation.php add this:

    function valid_date($date, $format){
    $d = DateTime::createFromFormat($format, $date);
    if($d && $d->format($format) == $date) {
    return true;
    } else {
    $this->set_message(‘valid_date’, ‘YOUR CUSTOM ERROR MESSAGE’);
    return false;
    }
    }

    And in controller use like this:
    $this->form_validation->set_rules(‘dob’, ‘DOB’, ‘required|valid_date[d-m-Y]’);
    You can change format paramer as you wish 🙂

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *

No spam? * Time limit is exhausted. Please reload CAPTCHA.