Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>i have had the same problem. One of the problems with the callback function is that it can only accept one parameter. there are two states to consider when checking for uniqueness of a record in your form. 1) you are adding a new record 2) you are editing an existing record.</p> <p>if you are adding a new record the inbuilt <em>is_unique</em> works fine. if you are editing an existing record <em>is_unique</em> does not work, because it finds the record you are editing and says the form data is not unique.</p> <p>to get around this problem i have used the session class, set it for case 2 before you run the validation script, so you need to know if you are editing an existing record, or adding a new record. to do this i just add a hidden input to the form when it is edited, eg the records unique id.<br> presumably you have a unique user id in your users table, eg so set it before the validation is run. </p> <p>if($this->input->post('user_id')){$this->session->set_userdata('callback_user_id',$this->input->post('user_id'));}</p> <p>then in your callback, use this sort of algorithm:</p> <p>case 1) ie $this->session->userdata('callback_user_id') == FALSE if the user name is unique, validate, and return true. if the user name is not unique, return false with validation message user has to be unique.</p> <p>case 2) ie, the callback_user_id is set. if the user name is unique, validate and return true if the user name is already set, and that record has the same id as user_id, that means you are updating the same record, and its fine to validate. otherwise, another record has the username, and it should fail validation. in the model i just have a method that returns the unique id for a username.</p> <p>after you run the validation, its probably a good idea to unset the callback_user_id session variable. i'm sorry i don't have code to paste, but i think this description should help you.</p> <p>==== edits nowadays, i think overriding the form validation with a new function is the way to go. so: have a language pack entry, a form validation line and the override: This assumes that a field is posted named ID that has the id of the row.</p> <pre><code>$lang['form_validation_is_unique_not_current'] ='The {field} field must contain a unique value.'; array('field' =&gt; 'username', 'label' =&gt; 'lang:…username…', 'rules' =&gt; 'trim|required|min_length[2]|max_length[40]|is_unique_not_current[users.username]'), class MY_Form_validation extends CI_Form_validation { function __construct($rules = array()) { parent::__construct($rules); $this-&gt;_error_prefix = '&lt;div class="alert alert-danger"&gt;&lt;p&gt;'; $this-&gt;_error_suffix = '&lt;/p&gt;&lt;/div&gt;'; } public function is_unique_not_current($str, $field) { sscanf($field, '%[^.].%[^.]', $table, $field); $id = $this-&gt;CI-&gt;input-&gt;post('id'); if($this-&gt;CI-&gt;input-&gt;post('field_name')) { return isset($this-&gt;CI-&gt;db) ? ($this-&gt;CI-&gt;db-&gt;limit(1)-&gt;get_where($table, array( $field =&gt; $str, 'id &lt;&gt; ' =&gt; $id))-&gt;num_rows() === 0) : FALSE; } return FALSE; } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload