CodeIgniter Forums

Full Version: form validation behavior
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
hello.

first sorry my english , it's not my native language.

I am using form validation successfully, but I have a problem with the rule is_unique when I want to edit the object. For example I have de user field with value "edel" ,of course it should be unique. When I load the view edit and I don't change the field user the form validation catch the error for this field because "edel" user exists in database but it's the same instance that I am editing.

so how can i avoid this behavior in editing views
You would need to create a verifyUser() method and check to see if the user is already in the database.
Hello, @Edel

I don't know if this helps you, but I've made a repo with custom form validation rules that I've used in the past at the following url: https://github.com/avenirer/MY_Form_validation-for-CI

You copy the MY_Form_validation.php inside your application/libraries directory and the my_form_validation_lang.php into your application/languages/english (or make your own validation language...).

After that, when you set the validation rule for your edited field you can use is_unique_except rule. In your particular case, assuming you have in your users table an id that uniquely identifies the row you want to edit, it would go on the lines of this:

PHP Code:
$this->form_validation->set_rules('user','Username','trim|required|is_unique_except[users.user.id.'.$id.']'

...where "users" is the table name, "user" is the field you want to edit, "id" is the name of the uniquely identifying column, and "$id" is the id of the row you want to edit.

You can of course delete the other form validation rules that are in there or even change the code and make it a callback rule. Hope this helped you.