Saturday, February 19, 2022

[FIXED] Use form_validation correctly in CodeIgniter

Issue

I have a registration system with CodeIgniter but currently I have no control on email and password. A user can register without putting email or a password. I have an index() and a register_user function() in my Signup controller but the redirection is not working on success At the moment I have the following code:

<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Signup extends CI_Controller {

    public function index()
    {

        if(!isset($this->session->userdata['sessiondata']['user_id']))
        {
             $this->load->helper(array('form', 'url'));

             $this->load->library('form_validation');
             $this->form_validation->set_rules('email', 'Email', 'required');
             $this->form_validation->set_rules('password', 'Password', 'required',
                    array('required' => 'You must provide a %s.')
             );
               if ($this->form_validation->run() == FALSE)
               {
                    $this->load->view('signup-view');
               }
               else
               {
                    $this->load->view('home-view');

               }

        }else{
            if (intval($this->session->userdata['sessiondata']['user_type']) == 1) {
                redirect(base_url().'admin');
            } else {
                redirect(base_url().'home');
            }
        }
    }

    function register_user(){

        $this->load->library('custom_u_id');

        $data = array('user_id' => $this->custom_u_id->construct_id('USR'),
                      'name' => $_POST['name'],
                      'email' => $_POST['email'],
                      'password' => $_POST['password'],
                     );

        $this->load->model('signup_model');
        $user_details = $this->signup_model->register_user($data);

        if (!empty($user_details)){

            $user_data =  array
            (
                'user_id' => $user_details['user_id'],
                'email' => $user_details['email'],
                'name' => $user_details['name'],
                'user_type' => $user_details['user_type'],

            );

            $this->session->set_userdata('sessiondata',$user_data);

            if (intval($user_details['user_type']) == 1) {
                redirect(base_url().'admin');
            } else {
                redirect(base_url().'home');
            }
        } else{
            redirect('login');
        }

    }// end of function login
}

Do I need to put the form_validation in my register_user function ? I've tried but the check doesn't work anymore...

I also have in my view the <?php validation_errors();?> function and the <?php form_open(base_url().'signup');?>


Solution

looking by your code, i think you want to put register_user() inside validation TRUE since the query is in that method.

so try to change your code to this :

public function index()
    {

        if(!isset($this->session->userdata['sessiondata']['user_id']))
        {
             $this->load->helper(array('form', 'url'));

             $this->load->library('form_validation');
             $this->form_validation->set_rules('email', 'Email', 'required');
             $this->form_validation->set_rules('password', 'Password', 'required',
                    array('required' => 'You must provide a %s.')
             );
               if ($this->form_validation->run() == FALSE)
               {
                    $this->load->view('signup-view');
               }
               else
               {
                    $this->register_user();

               }

        }else{
            if (intval($this->session->userdata['sessiondata']['user_type']) == 1) {
                redirect(base_url().'admin');
            } else {
                redirect(base_url().'home');
            }
        }
    }

and be sure your form action to like this :

<form action="<?=site_url('/signup/index');?>">


Answered By - Jazz

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.