php - how to check callback validation codeigniter duplicate database entry?

967

This is my callback function, I want to check the database for duplicate value, I have tried a lot, but I can't get validation to work. I'm new to Codeigniter so any help would be appreciated!

    public function alias_exist_check()
    {
        $scol_code = $this->input->post('school_code');
        $user_id=$this->input->post('user_id');
        $query=$this->db->get_where('user_application',array('school_code'=>$scol_code,                                                                                 'user_id'=>$user_id));
        $row= $query->row_array();

        if(!$row['user_id']==$user_id && !$row['school_code']==$scol_code)
        {
            return TRUE;

        } else {

            $this->form_validation->set_message('alias_exist_check', 'Already exists.');
            return FALSE;                    
        }
    } 

UPDATE1 :: i tried this but its not working me help me if i wrote any mistakes.

$this->form_validation->set_rules('school_code', 'School Name','required','callback_alias_exist_check', 'trim|xss_clean'); $where = array(
  'school_code' => $this->input->post('school_code'),
  'user_id' => $this->input->post('post'));

if( ! $this->lawschool_model->alias_exist_check($where))
{
      $this->form_validation->set_message('alias_exist_check', 'Already exists.');
                            }
     if ($this->form_validation->run() == FALSE)
    {
      $data['row']=  $this->lawschool_model->Getuser($data1);
      $data['row1']=  $this->lawschool_model->GetData1();
      $this->ag_auth->view('Home',$data);
    }
    else
    {
       $insert = $this->db->insert('user_application',$data);       

    if($insert==TRUE)
    {
        /*$idNum = $this->input->post('school_code');
        $data1 = $this->lawschool_model->upddata_school();*/

        $data['row'] =  $this->lawschool_model->Getuser($data1);
        $data['row1'] =  $this->lawschool_model->GetData1();
        $this->ag_auth->view('Home',$data);
     }
}

UPDATE2::finaly its works fine,here is my working code

   $this->form_validation->set_rules('school_code', 'School Name','required','callback_alias_exist_check1', 'trim|xss_clean'); 

   function alias_exist_check1($scol_code,$user_id)
   {
        $sql = "SELECT * FROM user_application WHERE school_code = ? AND user_id = ?";
        $val = $this->db->query($sql,array($scol_code ,$user_id ));

        if ($val->num_rows)
        {               
          $this->form_validation->set_message('alias_exist_check', 'Already exists.');
          return TRUE;
        }
        else
        {
          return FALSE;
        }
    }
130

Answer

Solution:

Model

public function alias_exist($where)
{
    return $this->db->where($where)->count_all_results('user_application') > 0;
}

Controller

public function alias_exist_check()
{
    $where = array(
        'school_code' => $this->input->post('school_code'),
        'user_id'     => $this->input->post('user_id')
    );
    return ! $this->name_model->alias_exist($where);
}
684

Answer

Solution:

The first function was not working because you tried to access post data from within the callback itself. This does not appear to work well with callbacks. This is because codeigniter will remove all post data from the request as soon as your run the form validator run method. It repopulates post data only when form processing is complete. Pass any extra parameters you need for you callback functions to work like this

callback_foo[bar]

People are also looking for solutions to the problem: javascript - How can I send YAML over AJAX?

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.