Updating Unique Field Validation for CI 1.7.0
Just a quick post to keep my CodeIgniter unique field validation extension up to date. When EllisLab released CodeIgniter 1.7.0, they included a new form validation class which is meant to replace the old validation class. The old class still exists in the code for now but it’s been deprecated, so developers are urged to use the new form validation class instead. Only very minor updates are needed to my validation extension in order to make it work with the new code. These were suggested to me by commenter libre before I even had a chance to look at implementing them myself. (Thanks libre!) For everyone’s benefit, here’s the updated version of the library (and remember, use this if you’re running CodeIgniter 1.7.0 or later:)
<?php if (!defined('BASEPATH')) exit('No direct script access allowed'); /** * MY_Form_validation Class * * Extends Form_Validation library * * Adds one validation rule, "unique" and accepts a * parameter, the name of the table and column that * you are checking, specified in the forum table.column * * Note that this update should be used with the * form_validation library introduced in CI 1.7.0 */ class MY_Form_validation extends CI_Form_validation { function My_Form_validation() { parent::CI_Form_validation(); } // -------------------------------------------------------------------- /** * Unique * * @access public * @param string * @param field * @return bool */ function unique($str, $field) { $CI =& get_instance();list($table, $column) = explode('.', $field, 2);$CI->form_validation->set_message('unique', 'The %s that you requested is unavailable.'); $query = $CI->db->query("SELECT COUNT(*) AS dupe FROM $table WHERE $column = '$str'"); $row = $query->row(); return ($row->dupe > 0) ? FALSE : TRUE; } } ?>
You’ll want to save this file as system/application/libraries/MY_Form_validation.php (instead of MY_Validation.php.)
When you load the library, don’t forget that it’s now called form_validation instead of just validation. There have also been some changes to the way you set your rules for the new form validation class. You can read all about this in the class documentation. Here’s the basic format, including usage of the “unique” rule from the extension:
$this->form_validation->set_rules('username','User Name','required|min_length[5]|unique[users.username]');
$this->form_validation->set_rules('emailaddress','Email Address','required|valid_email|unique[users.email]');

November 11th, 2008 at 2:20 pm
Hi again,
That code will not work with the rules in a config file (new in 1.7.0 too). The constructor needs a $rules = array() parameter to run properly.
Like I said in my last comment:
http://www.scottnelle.com/41/extending-codeigniters-validation-library/#comment-2289
Oh, and thanks for thanking me ;)
November 11th, 2008 at 2:55 pm
Hi libre,
I’m still putting my rules in the controller because that makes the most sense to me. Has using a config file for your rules become the new EllisLab-recommended method? I may have missed that.
November 11th, 2008 at 2:59 pm
Sorry, I misunderstood. I see that you were referring to setting the response language in a config file. I’ll have to take some time to evaluate that in more depth, and then I’ll add it to the code.
March 2nd, 2009 at 6:14 am
Very nice!!!!!!!!!!!! Thanks for code.
January 3rd, 2010 at 7:30 pm
split() has been DEPRECATED as of PHP 5.3.0 and REMOVED as of PHP 6.0.0
list($table, $column) = explode(‘.’, $field, 2);
January 3rd, 2010 at 9:24 pm
Thanks John. Looks like it’s been a while since I’ve reviewed this code. :-)
I’ve updated this post with your suggestion.
January 7th, 2010 at 6:58 am
Thank you for posting this! I’m new to CI and was going over the form validation documentation–I was curious on how to create a custom rule rather than using a callback function in set_rules().
Sorry if this a dumb question, but in your query the word dupe is an alias right? Is the keyword AS optional?
Also, let’s say I decided to make a callback function within the controller for a custom rule (as described in http://codeigniter.com/user_guide/libraries/form_validation.html#callbacks ).
If I wanted to utilize the “built-in” rules within the callback function, I can just call them by $this->form_validation->min_length($str, $val), right?
January 12th, 2010 at 10:43 pm
Yes, dupe is just an alias. AS is optional but I’ve edited the post to include it because even though it adds 3 bytes it makes everything much more clear. :)
It looks to me like the syntax you’ve outlined for using built in functionality in a callback should work fine, but I haven’t tested it.
February 15th, 2010 at 8:55 am
[...] más sobre esto en el blog del autor. Tags: codeigniter, extender, libreria, php You can follow any responses to this entry [...]
February 27th, 2010 at 12:32 pm
great additional to the ci universe. wondering, if you’ve given thought to handle combine unique validation when doing edits.