Code Igniter Date Validation
Those looking for a Code Igniter date validator rule can use the following custom validation. This is written for CI 1.7+
To use, copy the class below into your system/application/libraries folder named MY_Form_validation.php, then in your controller use it like any other rule.
Oh, and this is the first version so if you do come across bugs/requests let me know would you?
Cheers.
Form
This validation rule validates an input text box only right now.
<input type="text" name="date" value="<?php echo set_value('date'); ?>" size="10" />
Controller Usage
UK
$this->form_validation->set_rules('date', 'date', 'trim|required|valid_date[d/m/y,/]');
US
$this->form_validation->set_rules('date', 'date', 'trim|required|valid_date[m/d/y,/]');
Database
$this->form_validation->set_rules('date', 'date', 'trim|required|valid_date[y-m-d,-]');
The Code
<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
class MY_Form_validation extends CI_Form_validation {
function MY_Form_validation()
{
parent::CI_Form_validation();
}
/**
* @desc Validates a date format
* @params format,delimiter
* e.g. d/m/y,/ or y-m-d,-
*/
function valid_date($str, $params)
{
// setup
$CI =&amp;amp;amp;amp; get_instance();
$params = explode(',', $params);
$delimiter = $params[1];
$date_parts = explode($delimiter, $params[0]);
// get the index (0, 1 or 2) for each part
$di = $this->valid_date_part_index($date_parts, 'd');
$mi = $this->valid_date_part_index($date_parts, 'm');
$yi = $this->valid_date_part_index($date_parts, 'y');
// regex setup
$dre = "(0?1|0?2|0?3|0?4|0?5|0?6|0?7|0?8|0?9|10|11|12|13|14|15|16|17|18|19|20|21|22|23|24|25|26|27|28|29|30|31)";
$mre = "(0?1|0?2|0?3|0?4|0?5|0?6|0?7|0?8|0?9|10|11|12)";
$yre = "([0-9]{4})";
$red = '\\'.$delimiter; // escape delimiter for regex
$rex = "^[0]{$red}[1]{$red}[2]$";
// do replacements at correct positions
$rex = str_replace("[{$di}]", $dre, $rex);
$rex = str_replace("[{$mi}]", $mre, $rex);
$rex = str_replace("[{$yi}]", $yre, $rex);
if (ereg($rex, $str, $matches)) {
// skip 0 as it contains full match, check the date is logically valid
if (checkdate($matches[$mi + 1], $matches[$di + 1], $matches[$yi + 1])) {
return true;
} else {
// match but logically invalid
$CI->form_validation->set_message('valid_date', "The date is invalid.");
return false;
}
}
// no match
$CI->form_validation->set_message('valid_date', "The date format is invalid. Use {$params[0]}");
return false;
}
function valid_date_part_index($parts, $search) {
for ($i = 0; $i <= count($parts); $i++) {
if ($parts[$i] == $search) {
return $i;
}
}
}
}
| Print article | This entry was posted by admin on April 29, 2009 at 1:39 pm, and is filed under Code Igniter, Frameworks, PHP, Uncategorized, Web. Follow any responses to this post through RSS 2.0. You can leave a response or trackback from your own site. |
about 1 year ago
A simple and clean solution, yet flexible with the format parameter. I’ve used it on a client site. Thanks!
about 11 months ago
Hi,
I wanted to put this in a new file and use, instaed keeping the validation function in the same controller file. Please let me know, how I can put the code in library file and use it here.
Thank you,
about 9 months ago
you can, you need to use the MY_ style of overriding CI’s built in libraries. read up on the MY_ method, you’d want to call it MY_Formvalidation I think (whatever the normal validation lib is called)
about 9 months ago
Excellent just what I was looking for!