Welcome Guest, Not a member yet? Register   Sign In
How to add callback_ function settings using an array
#1

Hi

I have problem setting rules using an array (Link). I add callbacks (Link) and unfortunately  I receive an error like this "unable to access an error message corresponding you your field name Username. (blacklist)

Simple code for my library validation

PHP Code:
class Validation_lib{
 public function 
__construct(){
   $this->CI =& get_instance();
 }
 public function 
getRegister(){
  $getRegisterDetail = array(
            'username'      => $this->CI->input->post('username'),
            'email'         => $this->CI->input->post('user_email'),
            'pswd'          => $this->CI->hash_lib->auth_hash_pswd($this->CI->input->post('user_pswd')),             
            
'reg_datetime'  => date('Y-m-d H:i:s'),
        );
  $getRegisterValidation = array(
            array(
                'field'  => 'username',
                'label'  => 'Username',
                'rules'  => 'trim|required|is_unique[user.username]|callback_blacklist',
                'errors' => array(
                        'is_unique' => 'Username <b>'.$getRegisterDetail['username'].'</b> already exists.'
                    )
            ),
            array(
                'field'  => 'user_email',
                'label'  => 'E-mail',
                'rules'  => 'trim|required|valid_email|is_unique[user.email]'
                'errors' => array(
                        'is_unique' => 'Email <b>'.$getRegisterDetail['email'].'</b> already exists.'
                    )
            ),
            array(
                'field'  => 'user_pswd',
                'label'  => 'Password',
                'rules'  => 'trim|required',
            )
        );
        $getRegisterStatus = array(
            'reg_validation'    => $getRegisterValidation 
            'reg_detail'        => $getRegisterDetail,
        );
        return $getRegisterStatus;
 }
 public function 
callback_blacklist($str)
    {
        
        
if ($username == 'admin') {
            $this->CI->form_validation->set_message('blacklist''The field can not be the word "test"');
            return FALSE;
        } else {
            return TRUE;
        }
        
    
}

 
How to fix it? Callbacks is available in array?

Thanks!
This is me. JK not me.
Reply
#2

Change
PHP Code:
public function callback_blacklist($str

to:
PHP Code:
public function callback_blacklist($username
Reply
#3
Sad 

(09-15-2019, 05:10 AM)Wouter60 Wrote: Change
PHP Code:
public function callback_blacklist($str

to:
PHP Code:
public function callback_blacklist($username

thanks for reply and suggestion, unfortunately it is still not  working..
This is me. JK not me.
Reply
#4

(This post was last modified: 09-15-2019, 06:13 AM by Wouter60.)

Did you set the validation rules with:
PHP Code:
$this->CI->form_validation->set_rules($getRegisterValidation); 
?
Reply
#5

Your function should be called blacklist() not callback_blacklist().
Reply
#6

(This post was last modified: 09-17-2019, 02:37 AM by falcon812311.)

(09-15-2019, 08:33 AM)jreklund Wrote: Your function should be called blacklist() not callback_blacklist().

Hi, sorry late reply. Yes blacklist() not callback_blacklist(), I already tried it and didn't work. I wonder if callback_ function is not work in settings using an array...

(09-15-2019, 06:12 AM)Wouter60 Wrote: Did you set the validation rules with:
PHP Code:
$this->CI->form_validation->set_rules($getRegisterValidation); 
?

yes. Inside my controller.

Example code:
PHP Code:
public function register(){
   ....
   
//LOAD LIBRARY
   
$registerValidation $this->validation_lib->getRegister()['reg_validation'];
   
$this->form_validation->set_rules($registerValidation);
   ....

This is me. JK not me.
Reply
#7

@falcon812311,

CI callback reference: https://codeigniter.com/user_guide/libra...on-methods

While it may not matter...what version of CI are running?

In your validation array "callback_" should be the prefix to your validation function blacklist().

The actual function should be called blacklist().

Is this how your code is currently?
Reply




Theme © iAndrew 2016 - Forum software by © MyBB