Welcome Guest, Not a member yet? Register   Sign In
I want to return object from a function within a module to callback function
#1

[eluser]Unknown[/eluser]
This is my code inside controller:
Code:
public function login_validation(){
        #$this->load
        $this->form_validation->set_rules('login','Login','required|xss_clean|unique|trim|callback_can_log_in');
        $this->form_validation->set_rules('password','Password','required|trim');
        #this commented code not working
        /*if($this->form_validation->run()->role=='teacher'){
         #$this->load
         echo $this->form_validation->run()->id;
            exit('Teacher');
        } elseif ($this->form_validation->run()->role == 'student') {
         echo $this->form_validation->run()->id;
         exit('student');
        } elseif ($this->form_validation->run()==false) {
         exit('False');
        }*/
       echo $this->form_validation->run();#this code always returns 1
    }
    function can_log_in(){
     $this->load->model('site_model');
     return $this->site_model->can_log_in($this->input->post('login',true),$this->input->post('password',true));
    }

Code within my model:
Code:
function can_log_in($login,$password){
        $this->db->where('login',$login);
        $this->db->where('password',$password);
        $q = $this->db->get('users');
        
        if($q->num_rows()==1){
            foreach ($q->result() as $row) {
                if ($row->role == 'teacher') {
                    return (object) array('role' => $row->role, 'id' => $row->id);
                } elseif ($row->role == 'student') {
                    return (object)  array('role' => $row->role, 'id' => $row->id);#this is line 16
                }
            }
        } else {
            return false;
        }
    }

And, also I am getting this error:
Code:
A PHP Error was encountered

Severity: Notice

Message: Undefined property: stdClass::$id

Filename: models/site_model.php

Line Number: 16
#2

[eluser]LuckyFella73[/eluser]
Hi Buratino,

for the form validation callback function is just meant to know wether
the result is TRUE or FALSE I can't see a reason you want to return
an object from your model. Your controller function "can_log_in()"
should just return TRUE or FALSE.

Just a hint: prefix your controller function "can_log_in" with an
underscore to make it private and add one undersore where
you set the callback function as a rule. Otherwise that method can
be called directly:

Code:
$this->form_validation->set_rules('login','Login','required|xss_clean|unique|trim|callback__can_log_in');
$this->form_validation->set_rules('password','Password','required|trim');

function _can_log_in()
{
$this->load->model('site_model');
return $this->site_model->can_log_in($this->input->post('login',true),$this->input->post('password',true));
}
#3

[eluser]Unknown[/eluser]
Thank you, LuckyFella73 for your response and for advice.




Theme © iAndrew 2016 - Forum software by © MyBB