Welcome Guest, Not a member yet? Register   Sign In
Problem with validation an unique field
#1

[eluser]Mr.SoOoMa[/eluser]
Hello there ..

I use the form validation library to validate my fields in a form .. but i have a problem with unique field ..

I have a table for "Clients" and every table has email field, and i need the email to be unique ..

in (add client) form i have no problem to put rules like this :

Code:
$this->form_validation->set_rules('email', 'lang:email', 'required|valid_email|is_unique[clients.email]');

and the input is :

Code:
<input type="text" name="email" size="50" value="{set_value('email')}" />

till now, i have no problem, but my problem is in (edit client) form .

when i edit a client i select all of fields from database and i put each one in the form like this :

Code:
<input type="text" name="email" size="50" value="{$client.email}" />

if the user edited the value it will update, if not the form will replace the old value with the same value.

my problem is, when i put the rule (is_unique), the user can't put the same email and save "actually he won't change the email Smile" ..

and if i don't put the rule (is_unique), the user can register a new client with unique email, and edit it later with non unique email, it's a bug of course Big Grin

what should i do in that ?
#2

[eluser]CroNiX[/eluser]
Yes, you'd have to make your own callback validation function for editing or extend the validation class with your own rule that will ignore it for the current user id.
#3

[eluser]Mr.SoOoMa[/eluser]
nice, i made this and it works greeeeeeeeeat dude Big Grin

validation page :

Code:
$this->form_validation->set_rules('email', 'lang:email', "callback_edit_unique_email[$id]");
public  function edit_unique_email($email,$id){
        if($this->clients->if_email_unique($email,$id)==0){
            return true;
        }
        else{
            return false;
        }
    }

model :

Code:
public  function edit_unique_email($email,$id){
        if($this->clients->if_email_unique($email,$id)==0){
            return true;
        }
        else{
            return false;
        }
    }

and i add the error msg to the system file .. it works great but i have a small problem ..

how can i put my own validation function -the function not the rules- in an external file .. because i have to use it in more than one form .. i try to put it in a helper but it didn't work Sad

#4

[eluser]CroNiX[/eluser]
See the Extending Core classes section and create a MY_Form_validation class extension.

The only difference is when setting your message, you'd use $this->set_message($rule_name, $message) instead of $this->form_validation->set_message() since $this is the form_validation class when extending it.
#5

[eluser]Mr.SoOoMa[/eluser]
i've created a file with the name : MY_Form_validator.php in the folder application/libraries ..

and the content is :

Code:
<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
class MY_Form_validation extends CI_Form_validation {

    function __construct()
    {
        parent::__construct();
    }
      public  function edit_unique_email($email,$id){
        $this->load->model("Clients_model","clients");
        if($this->clients->if_email_unique($email,$id)==0){
            return true;
        }
        else{
            return false;
        }
    }
    
    }

i tried $this->form_validation->set_rules('email', 'lang:email', "callback_edit_unique_email[$id]");

but it didn't work, sorry but i'm new in CI :$ ...
#6

[eluser]CroNiX[/eluser]
Its no longer a callback since you extended the parent class. Now you can use just the rule name just like any of the other CI Rules (without "callback_").

Also, the file name should be "MY_Form_validation.php" and not "MY_Form_validator.php"
#7

[eluser]Mr.SoOoMa[/eluser]
i did that, but now i have this problem :
Fatal error: Call to a member function model() on a non-object in C:\AppServ\www\clever\application\libraries\MY_Form_validation.php on line 9

i can't load any models .. but i have to , because i have to select rows from database to my validation function ..
#8

[eluser]CroNiX[/eluser]
You should really read the user guide.
See the Utilizing CodeIgniter Resources within Your Library section
#9

[eluser]Mr.SoOoMa[/eluser]
i know that, i read it before but i didn't understand all of it because i told you that i'm new in CI & english isn't my native language Sad

i changed it to :

Code:
<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
class MY_Form_validation extends CI_Form_validation {

    function __construct()
    {
        parent::__construct();
        $CI =& get_instance();
        $CI->load->model("Clients_model","clients");
    }
      public  function edit_unique_email($email,$id){
        if($CI->clients->if_email_unique($email,$id)==0){
            return true;
        }
        else{
            return false;
        }
    }
    
    }

but i have the problem : Fatal error: Call to a member function if_email_unique() on a non-object in C:\AppServ\www\clever\application\libraries\MY_Form_validation.php on line 11

please can u help me Sad
#10

[eluser]CroNiX[/eluser]
This doesn't have to do with CodeIgniter. This has to do with PHP, classes, variables/properties and scope.

In your edit_unique_email function, everywhere you are using $CI change it to $this->CI.




Theme © iAndrew 2016 - Forum software by © MyBB