Scott Nellé.com

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]');
		

10 Responses to “Updating Unique Field Validation for CI 1.7.0”

  1. libre Says:

    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 ;)

  2. Scott Nellé Says:

    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.

  3. Scott Nellé Says:

    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.

  4. Pramod Poudel Says:

    Very nice!!!!!!!!!!!! Thanks for code.

  5. John Steele Says:

    split() has been DEPRECATED as of PHP 5.3.0 and REMOVED as of PHP 6.0.0

    list($table, $column) = explode(‘.’, $field, 2);

  6. Scott Nelle Says:

    Thanks John. Looks like it’s been a while since I’ve reviewed this code. :-)

    I’ve updated this post with your suggestion.

  7. CuDDL Says:

    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?

  8. Scott Nelle Says:

    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.

  9. Extender la validación de Codeigniter- SirViejo Says:

    [...] más sobre esto en el blog del autor. Tags: codeigniter, extender, libreria, php You can follow any responses to this entry [...]

  10. Richard Says:

    great additional to the ci universe. wondering, if you’ve given thought to handle combine unique validation when doing edits.

Leave a Reply