0
votes

I have created the signup form including the following fields like,

  • name
  • email
  • role
  • password
  • Confirm password

But the form validation was not working

I tried a lot but doesn't works is that i want to change anything in Config.php i have already changed the $config[library],$config[helper] can any one help me in this code. Thank you in advance

This is my Controller

<?php

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

class Login extends CI_Controller {

    public function __construct()
    {
        parent::__construct();
        $this->load->model('Mod_saloon');
        $this->load->helper(array('form', 'url'));
        $this->load->library(array('session', 'form_validation', 'email')); 
        $this->load->helper('url');
    }
    public function index()
    {
        $this->load->view('login');
    }
    public function show_signup()
    {
        $this->load->view('signup');
    }
    public function signup()
    {
        $config = array(
        array(
                'field' => 'name',
                'label' => 'Username',
                'rules' => 'required|min_length[5]|max_length[12]|is_unique[signup.name]'
        ),
        array(
                'field' => 'password',
                'label' => 'Password',
                'rules' => 'required',
                'errors' => array(
                        'required' => 'You must provide a %s.',
                ),
        ),
        array(
                'field' => 'cpassword',
                'label' => 'Password Confirmation',
                'rules' => 'required|matches[password]'
        ),
        array(
                'field' => 'email',
                'label' => 'Email',
                'rules' => 'required|valid_email|is_unique[signup.email]'
        )
);

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

        $this->form_validation->set_rules(
        'name', 'Username',
        'required|min_length[5]|max_length[12]|is_unique[signup.name]',
        array(
            'required'      => 'You have not provided %s.',
            'is_unique'     => 'This %s already exists.'
        )
        );
        $this->form_validation->set_rules('password', 'Password', 'required');
        $this->form_validation->set_rules('cpassword', 'Password Confirmation', 'required|matches[password]');
        $this->form_validation->set_rules('email', 'Email', 'required|valid_email|is_unique[signup.email]');
        if ($this->form_validation->run() == FALSE) 
        {
            echo "<script>alert('Validation Error');</script>";
            $this->load->view('signup');
        } 
        else 
        {
            $name = NULL;
            $role=NULL;
            $email=NULL;
            $password=NULL;
            $cpassword=NULL;

            extract($_POST);

            $signup['$name'] = $name;
            $signup['$role'] = $role;
            $signup['$email'] = $email;
            $signup['$password'] = $password;
            $signup['$cpassword'] = $cpassword;

            $this->Mod_saloon->new_signup($signup);

            $this->load->view('signup');
        }


    }
}

This is my View

<form action="signup" method="post">
            <div class="form-group has-feedback">
                <input type="text" class="form-control" name="name" placeholder="Name">
                <span class="glyphicon glyphicon-user form-control-feedback"></span>
            </div>
            <select class="form-control" name="role">
                <option disabled selected value> --Select the Role-- </option>
                <option value="admin">Admin</option>
                <option value="user">User</option>
            </select>
            <div class="form-group has-feedback">
                <input type="email" class="form-control" name="email" placeholder="Email">
                <span class="glyphicon glyphicon-envelope form-control-feedback"></span>
            </div>
            <div class="form-group has-feedback">
                <input type="password" class="form-control" name="password" placeholder="Password">
                <span class="glyphicon glyphicon-lock form-control-feedback"></span>
            </div>
            <div class="form-group has-feedback">
                <input type="password" class="form-control" name="cpassword" placeholder="Confirm Password">
                <span class="glyphicon glyphicon-lock form-control-feedback"></span>
            </div>
            <div class="gap"></div>
            <button type="submit" class="btn btn-primary btn-block btn-flat centered" style="width:35%;">Create User</button>
        </form>
3

3 Answers

0
votes

I think that your problem is not that your form validation is not working...in your controller your "signup" function does not seem to run anywhere.You need to check your routes and in the function that is used to run your link you need to call

$this->signup

You also need to call it on form submission so in your view the submit button must have a name and a value(eg: name="create_user",value="something") signup you should have

If ($this->input->post('create_user')){do the form validation thing}

0
votes

change action in form like

<form action="<?php echo base_url()?>index.php/Login/signup" method="post">

and there is no need to set rules multiple times. either you can use array method, or set rules individually

0
votes

Your code needs lots of correction

  1. Form Tag

Your Need to change the form tag first:

<form action="<?= base_url('login/signup') ?>" method="post">
  1. Put Your Config array inside config folder. Add Your Config array in form_validation.php add below code in this file.

application/config/form_validation.php

$config = array(
    'signup_validation' => array(
        array(
                'field' => 'name',
                'label' => 'Username',
                'rules' => 'required|min_length[5]|max_length[12]|is_unique[signup.name]'
        ),
        array(
                'field' => 'password',
                'label' => 'Password',
                'rules' => 'required',
                'errors' => array(
                        'required' => 'You must provide a %s.',
                ),
        ),
        array(
                'field' => 'cpassword',
                'label' => 'Password Confirmation',
                'rules' => 'required|matches[password]'
        ),
        array(
                'field' => 'email',
                'label' => 'Email',
                'rules' => 'required|valid_email|is_unique[signup.email]'
        )
    )
    );
  1. Your Controller function

Here is the code of your controller function

public function signup()
{

    if ($this->form_validation->run('signup_validation') == TRUE) {

        // Your sign up the code if validation is true. 

    } else {
        // load the view 


        //User validation_errors(); to show all your validation error at view
    }
}