• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Codeigniter Passing Extra Parameters to Custom Validation Rule

#1
According to this documentation, how can I pass second parameter to the rule method?

This is my custom rule


PHP Code:
public function email_exists($email$exclude_id=NULL)
{
 
   if $exclude_id !== NULL $this->db->where_not_in('id'$exclude_id);

 
   $result $this->db->select('id')->from('users')->where('email'$email)->get();

 
   if $result->num_rows() > ) {
 
       $this->form_validation->set_message('email_exists''{field} has been used by other user.');
 
       return FALSE;
 
   } else {
 
       return TRUE;
 
   }


and this is how i call it from controller


PHP Code:
$rules = [
 
   [
 
       'field' => 'email',
 
       'label' => 'Email',
 
       'rules' => [
 
           'required',
 
           'trim',
 
           'valid_email',
 
           'xss_clean',
 
           ['email_exists', [$this->m_user'email_exists']]
 
       ]
 
   ]
];

$this->form_validation->set_rules($rules); 

How can I pass second parameter to email_exists method?
Reply

#2
Maybe something like this:


PHP Code:
$second_param 'whatever';

$rules = [
    [
        'field' => 'email',
        'label' => 'Email',
        'rules' => [
            'required',
            'trim',
            'valid_email',
            'xss_clean',
            [
                'email_exists'
                function( $str ) use ( $second_param ){
                    return $this->m_user->email_exists$str$second_param ); 
                }
            ]
        ]
    ]
]; 
Reply

#3
(07-17-2017, 12:07 PM)skunkbad Wrote: Maybe something like this:


PHP Code:
$second_param 'whatever';

$rules = [
    [
        'field' => 'email',
        'label' => 'Email',
        'rules' => [
            'required',
            'trim',
            'valid_email',
            'xss_clean',
            [
                'email_exists'
                function( $str ) use ( $second_param ){
                    return $this->m_user->email_exists$str$second_param ); 
                }
            ]
        ]
    ]
]; 

It works. Thanks a lot sir.
Reply


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


Users browsing this thread:
1 Guest(s)


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2017 MyBB Group.