Welcome Guest, Not a member yet? Register   Sign In
call method of the controller
#1

[eluser]Thiago Leao[/eluser]
Hi friend, i need help.

I am calling a method specifying the controller, within the model.

Controller:

Code:
function index(){
        
        $dados['titulo'] = 'Catalogo Colletion - Sua coleção Online!';        
        
        $this->load->model('home_model');
        $dados['categorys'] = $this->home_model->listar_category();
        
        $this->load->view('registrar', $dados);
    }
    
    function insert(){
        
        $this->load->library('form_validation');
        $config = array(
            array('field' => 'nome', 'label' => 'nome', 'rules' => 'required'),
            array('field' => 'login', 'label' => 'login', 'rules' => 'required'),
            array('field' => 'senha', 'label' => 'Senha', 'rules' => 'required')
        );
        $this->form_validation->set_rules($config);
        
        if($this->form_validation->run() == FALSE){
            
            $this->index();
            
        }else{
    
            $data['nome']     = $this->input->post('nome');
            $data['login']     = $this->input->post('login');
            $data['senha']     = $this->input->post('senha');
            
            $this->load->model('registrar_model');
            $this->registrar_model->insert($data);
            $this->sucess();
        }
    
    }

Model

Code:
function insert($data){    
    
        $query = $this->db->get_where('users', array('login' => $data['login']));
        
        if ($query->num_rows() != 0){
            CALL METHOD OF THE CONTROLLER
        }else{
            
            $array = array('nome'=> $data['nome'], 'login' => $data['login'], 'senha' => $data['senha'], 'date' => date('Y-m-d'), 'session_date' => date('Y-m-d h:m:s'));
            $this->db->set($array);
            $this->db->insert('users');
        }
    }
#2

[eluser]mdvaldosta[/eluser]
You can do a redirect back to index like redirect('index', 'location') instead of trying to call it. When doing form validation though, it's usually best to just reload the view with the form if validation is false.
#3

[eluser]Thiago Leao[/eluser]
But what if I want to give a redirect to another page? will give an error!
Or give an alert saying that the User is already registered?

thanks friend!
#4

[eluser]mdvaldosta[/eluser]
Remove the insert, put all that code in the index and when form validation == FALSE load everything you have in index() now. When you first load the page it will validate FALSE and run. All of that should be in one method.

Or, take everything out of index and put it where your calling $this->index() as that would be the same thing.




Theme © iAndrew 2016 - Forum software by © MyBB