[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;
}
}