CodeIgniter Forums
problem in form_validation using is_callable(), cannot passing data from controller - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forumdisplay.php?fid=5)
+--- Forum: General Help (https://forum.codeigniter.com/forumdisplay.php?fid=24)
+--- Thread: problem in form_validation using is_callable(), cannot passing data from controller (/showthread.php?tid=73361)



problem in form_validation using is_callable(), cannot passing data from controller - DELE - 04-15-2019

I separate all processing data models in libraries, the model is only used to handle data from the database only and the controller is specialized to control requests.

I get a problem when processing data in library_models because of the limitations of calling methods in model_joins, so I tried experimenting so that it became a method below.
controller:
PHP Code:
$this->form_validation->set_rules(
    
'email',
    
'Email',
    [
        
'required',
        [
            
'checkEmail',
            [
                
$this->library_models->check_email($this->model_joins->user_campaign('user_email'$this->input->post('email'))) // problem here
            
]
        ],
        [
            
'checkRole',
            [
                
$this->library_models->check_role($this->model_joins->user_campaign('user_email'$this->input->post('email'))) // problem here
            
]
        ]
    ],
    [
        
'checkEmail'    => 'Email not exists',
        
'checkRole'    => 'Account is not activef'
    
]
); 

libraries:
PHP Code:
public function check_role($email){
    if (
$email->user_role == 0) {
        return 
false;
    }
    return 
true;


models:
PHP Code:
   public function user_campaign($key$value)
 
   {
 
       return $this->db->query("
            SELECT *
            FROM (
                SELECT *
                FROM users
                WHERE 
$key = '$value'
            ) users_campaigns
            INNER JOIN campaigns
            ON users_campaigns.id = campaigns.id
        "
)->row();
 
   

but the results failed, because the above method does not return any values so that it displays the message "Trying to get property 'user_role' of non-object".

please help me to solve this problem.


RE: problem in form_validation using is_callable() - dave friend - 04-15-2019

You're going to have to show us the code for model_joins::user_campaign before we can offer much advice. Clearly though the problem is that model_joins::user_campaign does not return an object with a user_role method.


RE: problem in form_validation using is_callable() - DELE - 04-15-2019

(04-15-2019, 07:15 PM)dave friend Wrote: You're going to have to show us the code for model_joins::user_campaign before we can offer much advice. Clearly though the problem is that model_joins::user_campaign does not return an object with a user_role method.

I think there is no problem with model_joins :: user_campaign. but I have updated my post.


RE: problem in form_validation using is_callable() - dave friend - 04-15-2019

(04-15-2019, 07:40 PM)DELE Wrote: I think there is no problem with model_joins :: user_campaign. but I have updated my post.

There is a problem because user_campaign is not returning an object. This will be the case when CI_DB_result::row() returns null. It returns null when there are no records returned by the query.


RE: problem in form_validation using is_callable() - DELE - 04-15-2019

(04-15-2019, 08:10 PM)dave friend Wrote:
(04-15-2019, 07:40 PM)DELE Wrote: I think there is no problem with model_joins :: user_campaign. but I have updated my post.

There is a problem because user_campaign is not returning an object. This will be the case when CI_DB_result::row() returns null. It returns null when there are no records returned by the query.

why does it not return object values? instead returns a null value? and how do I overcome that using the is_callable() method?

I tried the callback method before using a query from user_campaign and it worked well without any problems.


RE: problem in form_validation using is_callable() - dave friend - 04-16-2019

(04-15-2019, 09:49 PM)DELE Wrote: why does it not return object values? instead returns a null value? and how do I overcome that using the is_callable() method?

It returns no object because no record in the database satisfied the where condition. In other words, there is no data to turn into an object.

Perhaps the easiest solution is to have the callback respond in a logical way when its argument is null.

Oh... and for what it's worth. The following

PHP Code:
public function check_role($email){
 
   if ($email->user_role == 0) {
 
       return false;
 
   }
 
   return true;



would be much more concise if written like this

PHP Code:
public function check_role($email){
 
   return $email->user_role != 0;


Or, if you want to account for the argument possibly being null

PHP Code:
public function check_role($email){
 
   return ! empty($email->user_role);




RE: problem in form_validation using is_callable() - DELE - 04-16-2019

It seems like you failed to understand my point. actually there are no problems in my libraries and models. I have tested everything.

I asked how to pass data from the controller using form_validation with is_callable().


the problem is here.
https://www.codeigniter.com/user_guide/libraries/form_validation.html?highlight=form%20validation#callable-use-anything-as-a-rule
he only passed data from $_post