CodeIgniter Forums

Full Version: Form Validation: Custom Callback Rule Problem...
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hey--I've set up a custom Form Validation Callback fairly similar to the "username" example shown in the Manual. 

Everything seems to be working great except when I load the page(this is the 2nd page of the form) I have the Error message come up on the First attempt. 

Why is this happening and what can I do to avoid this?

Code for setting callback up:
$this->form_validation->set_rules
('header_1', 'Header 1', 'callback_bigsearch_check');

Callback Function:
function bigsearch_check($header_1)
{
$query_example = $this->db->query("
SELECT
*
FROM
Table
WHERE
Header = '$header_1'
");

if($query_example->num_rows() == 0)
{
$this->form_validation->set_message(
'bigsearch_check',
'That Item is not Found in the Database,
Please Select One from the dropdown'
);

return FALSE;
}
else
{
return TRUE;
}
}


Callback function

~Ryan
You are doing something wrong with the run() method.
PHP Code:
if( $this->form_validation->run() === TRUE )
{
    
// Everything OK
}
else
{
    
// First run, load view.
    
$this->load->view('view',$data);


You also got a SQL Injection in your code. Use Query binding.
https://www.codeigniter.com/user_guide/d...y-bindings
You mention that this is the 2nd page of your form. I guess the form_validation rule is applied after you post the 1st page.
Please, share the logic in your controller that handles the form pages and $_POST result.
And please, don't paste your php code as plain text in the forum editor. There's a button for php code in the button bar. If you use that, you make the code much better readable for everyone.