Im trying to overwrite the is_unique rule, and make it so you can either use as it is, or do something like:
is_unique[table.field|value_to_ignore]
Example:
PHP Code:
$this->form_validation->set_rules('Page_Title', 'page_title', "is_unique[pages.title|{$current_title}]");
This way it can be used to check for unique values, but if its the current value, it wont say its an existing value.
The problem though, is it doesnt seem to be working.. The error that gets returned from the validation is:
Unable to access an error message corresponding to your field name Page Title.(is_unique[pages.title)
Heres my code thus far.
MY_Form_validation.php
PHP Code:
<?php defined('BASEPATH') OR exit('No direct script access allowed');
class MY_Form_validation extends CI_Form_validation {
public function is_unique($str, $field)
{
if( ! isset($this->CI->db))
return FALSE;
if( preg_match('/^(.*)\.(.*)\|(.*)$/', $field, $matches) !== FALSE )
{
$table = $matches[1];
$field = $matches[2];
$ignore = $matches[3];
}
elseif( preg_match('/^(.*)\.(.*)$/', $field, $matches) !== FALSE )
{
$table = $matches[1];
$field = $matches[2];
$ignore = NULL;
}
else
{
return FALSE;
}
$this->CI->db
->select('*')
->from($table)
->where($field, $str);
if( ! is_null($ignore))
$this->CI->db->where_not($field, $ignore);
return $this->CI->db->num_rows() === 0;
}
}
form_validation_lang.php
PHP Code:
<?php
$lang['form_validation_is_unique'] = 'not unique';
Controller Validation Snippet
PHP Code:
...
$this->form_validation->set_rules(
'page_title', 'Page Title', "required|alpha_numeric_spaces_dash|min_length[5]|max_length[50]|is_unique[pages.title|{$data['settings']->title}]",
array(
'required' => 'Need to specify a unique page title',
'is_unique' => 'The page title specified already exists for another page'
)
);
...
Im guessing that theres something that checks the regex of the rule string before it passes it to the form validation rules...