Welcome Guest, Not a member yet? Register   Sign In
Form Validation Callback
#1

[eluser]xtremer360[/eluser]
I'm attempting to validate an input field for the title_name. I created a callback function to report back if the user input already exists as a name in the database or not but every time it reports back that it does exist. Even when there's nothing in the database. The call to the function save is from a jquery ajax request.

Code:
public function save()
{
    $output_status = 'Error';
    $output_title = 'Not processed';
    $output_message = 'The request was unprocessed!';
  
    $this->form_validation->set_rules('title_name', 'Title Name', 'required|trim|xss_clean|callback_title_name_check');
  
    if ($this->form_validation->run())
    {
        $title_saved = $this->titles_model->save_title($this->input->post('title_name'), $this->input->post('title_directory_name'), $this->input->post('title_status'));
        if ($title_saved)
        {
            $output_status = 'Success';
            $output_title = 'Title Created';
            $output_message = 'Title was successfully created!';
        }
        else
        {
            $output_title = 'Title Not Created';
            $output_message = 'Title was not successfully created!';
        }
    }
    else
    {
        $output_title = 'Form Not Validated';
        $output_message = validation_errors();
    }
    echo json_encode(array('status' => $output_status, 'title' => $output_title, 'message' => $output_message));

Code:
public function title_name_check($title_name)
{
    $title_name_exists = $this->titles_model->get_title(array('title_name' =>$title_name));
    if (is_null($title_name_exists))
    {
        $this->form_validation->set_message('title_name_check', 'The title name already exists!');
        return FALSE;
    }
    else
    {
        return TRUE;
    }
}

Code:
public function get_title($params = array())
{
    $this->db->select('titles.title_id');
    $this->db->select('titles.title_name');
    $this->db->select('titles.title_directory_name');
    $this->db->select('titles.title_sort_order');
    $this->db->select('title_statuses.title_status_name');

    $this->db->from('titles');
    $this->db->join('title_statuses', 'titles.title_status_id = title_statuses.title_status_id');
  
    //checking to see if any $params are attempting to be passed
    if (count($params) > 0)
    {
        //start title name specific selection
        if (isset($params['title_name']))
        {
            $this->db->where('titles.title_name', $params['title_name']);
        }
  
        //start title name specific selection
        if (isset($params['title_directory_name']))
        {
            $this->db->where('titles.title_directory_name', $params['title_name']);
        }
    }
    $query = $this->db->get();
    echo $this->db->last_query();
    if ($query->num_rows() > 0)
    {
        return $query->row();
    }
    else
    {
        return NULL;
    }
}
#2

[eluser]xtremer360[/eluser]
Any ideas?
#3

[eluser]Aken[/eluser]
Edit: CroNIX found the issue, I missed it, too. See below. Smile
#4

[eluser]CroNiX[/eluser]
Yeah, the logic is not sound.

in get_title()
Code:
//this is saying if it exists, return the first row
if ($query->num_rows() > 0)
{
  return $query->row();
}
else
{
  //if it DOESNT exist, return null
  return NULL;
}

in the callback title_name_check(), you are checking to see if it's null to determine if it exists, which isn't what your model is doing.
Code:
if (is_null($title_name_exists))
{
  $this->form_validation->set_message('title_name_check', 'The title name already exists!');
  return FALSE;
}
else
{
  return TRUE;
}
#5

[eluser]xtremer360[/eluser]
But that makes sense to me though if the title name exists would be false/ meaning Null in this instance and would return NULL so how is it wrong.
#6

[eluser]Aken[/eluser]
Your callback needs to return TRUE if there are zero errors, or FALSE if there are one or more errors. In your case, an existing title should throw an error, but you are returning FALSE for when there is no existing title (or, when your model is returning null).
#7

[eluser]xtremer360[/eluser]
I switched the TRUE and FALSE inside the if (is_null()) { statement and it still is not working.




Theme © iAndrew 2016 - Forum software by © MyBB