I'm trying to figure out how I can use the is_unique
rule from the Codeigniter form validation library in the following situation.
I'm trying to submit a edit user form and have the rule:
$this->form_validation->set_rules('user_name', 'User Name', 'required|trim|xss_clean|is_unique[users.user_name]');
What if other values in the form are being changed but this value stays the same. The form is going to see that this value already exists so how would I protect it from editing if this value isn't changed.
Using your code as an example, the
is_unique
validation rule works by looking for a field calleduser_name
in yourusers
database table. If the field with the same value exists it validates as false.To make sure it runs only when the user submits a new value, you could check the posted value
$this->input->post('user_name')
against the value you pulled from the database to populate your form with. If they are the same, don't validate is_unique;