• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
form validation behavior

#1
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
Reply

#2
You would need to create a verifyUser() method and check to see if the user is already in the database.
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply

#3
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.
Reply


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2019 MyBB Group.