2

I am separating the logic of defining form validation rules into a library. i want to apply multiple callback functions on a single form element.

$this->form_validation->set_rules('email', 'email', 'callback_db_check|callback_valid_email');          

Now i don't know how to do this. Because its not working i mean multiple callback are not working. But if i define single callback its working fine.

function db_check(){
    $this->CI->form_validation->set_message('db_check', 'Not found in db');
}

function valid_email(){
    $this->CI->form_validation->set_message('db_check', 'Invalid email');
}

This is just example code. I have extended form validation library so that i can define and call validation logic from my library . Any suggestion how i can do it?

2 Answers 2

3
$this->CI->form_validation->set_rules('email' , 'Email' , 'required|valid_email|max_length[255]|callback_email|callback_call_db'); 

p.s. your callback should always return true or false (your example doesnt return anything)

Sign up to request clarification or add additional context in comments.

1 Comment

OK yes returning true or false on each function is working fine thanks but the syntex is liek this i have $this->CI->form_validation->set_rules('email' , 'Email' , 'required|valid_email|max_length[255]|callback_email|callback_call_db');
3

You can apply multiple callback functions in a single element

Validation rule can be set by using the format

$this->form_validation->set_rules('email', 'email', 'callback_db_check|callback_valid_email');

and the callback functions must return true or false .

function db_check($user) 
     {

        $sql=$this->db->query("select * from user where email like '%$user%' ");
        if($sql->num_rows()>0)
        {
             return true;
        }
             else
        {

        $this->form_validation->set_message('db_check', 'Not found in db');
        return FALSE;
        }

     }




 function valid_email($user) 
      {


            if(//condition)
            {
                 return true;
            }
                      else
            {

            $this->form_validation->set_message('valid_email', 'In valid email');
            return FALSE;
            }

      }

In user side

<div class="field_main">
<div class="fi_title">E-Mail Address *:</div>
<input type="text" id="email" name="email"  value="<? echo set_value('email');?>" class="field_class" />
<? echo form_error('email','<div class="error">', '</div>');?>
</div>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.