CodeIgniter Forums

Full Version: Form validation callbacks not working
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hello,

After upgrade from 2.2.6 to 3.1.9. I noticed my callback function for form validation not working.

PHP Code:
function update_password()
    {
        
        if(
$this->session->userdata('recovery')!='yes')
        
$this->form_validation->set_rules('current_password''Current Password''callback_currentpass_check');
        
        
$this->form_validation->set_rules('new_password''New Password''required|matches[re_password]');
        
$this->form_validation->set_rules('re_password''Password Confirmation''required');
            
        if (
$this->form_validation->run() == FALSE)
        {
            
$this->changepass();    
        }
        else
        {
            
                
$password $this->input->post('new_password');
                
$this->auth_model->update_password($password);
                
$this->session->set_userdata('recovery',"no");
                
$this->session->set_flashdata('msg''<div class="alert alert-success">Password changed successfully</div>');                
            
            
redirect(site_url('admin/auth/changepass'));        
        }
    
    } 



PHP Code:
public function currentpass_check($str)
    {
        
$user_name 'test_user';
        
$res $this->auth_model->check_login($user_name,$str);
        
        if (
$res == 0)
        {
            
$this->form_validation->set_message('currentpass_check''Current password Didn\'t match');
            return 
FALSE;
        }
        else
        {
            return 
TRUE;
            
        }
    } 

I am getting this error.
[Image: 50575236-2500a200-0e03-11e9-97ae-d19325f4aa60.png]
My callback function ( currentpass_check) returns TRUE.
Instead of 
PHP Code:
$this->form_validation->set_message('currentpass_check''Current password Didn\'t match'); 
try this:

PHP Code:
$this->form_validation->set_message('current_password''Current password Didn\'t match'); 

untested...
@codehoster,

...or simply try using the double quotes.


PHP Code:
$this->form_validation->set_message('current_password'"Current password Didn't match"); 
(01-02-2019, 11:34 AM)Codinglander Wrote: [ -> ]Instead of 
PHP Code:
$this->form_validation->set_message('currentpass_check''Current password Didn\'t match'); 
try this:

PHP Code:
$this->form_validation->set_message('current_password''Current password Didn\'t match'); 

untested...

Same error!

https://www.codeigniter.com/userguide3/l...ation.html

In documentation your recommendation is not valid.
Try changing it to this:

PHP Code:
$this->form_validation->set_message('current_password''Current password do not match'); 
Are you absolutely sure that it returns TRUE? It dosen't need to access a message in case it's TRUE, only on FALSE.
Are these function placed in the same .php file or are you extending it?
Yes I am absolutely sure!
@Codehoster,

Did you try the double quotes? Just wondering?
The set_message method allows you to set your own error messages on the fly. But one thing you should notice is that the key name has to match the function name that it corresponds to.

If you need to modify your custom rule, which is _check_valid_username, you can do so by perform set_message within this function:

function _check_valid_username($str)
{
// Your validation code
// ...
// Put this in condition where you want to return FALSE
$this->form_validation->set_message('_check_valid_username', 'Error Message');
//
}
If you want to change the default error message for a specific rule, you can do so by invoking set_message with the first parameter as the rule name and the second parameter as your custom error. E.g., if you want to change the required error :

$this->form_validation->set_message('required', 'Oops this %s is required');
If by any chance you need to change the language instead of the error statement itself, create your own form_validation_lang.php and put it into the proper language folder inside your system language directory.