phpcodeigniterformvalidation-plugin

Form validation is not working in my CI project (fixed, code updated)


I am newbie at CodeIgniter. I am trying to implement form validation in user registration.

$autoload['libraries'] = array('database','session','encrypt','form_validation');

$autoload['helper'] = array('url','file','form');

$autoload['model'] = array('user_model');

Controller's Code:

class User_registration extends CI_Controller {

public function index() {

    $this->register();
}

function register() {

    //set validation rules
    $this->load->library('form_validation');

    $this->form_validation->set_rules('user_name', 'First Name', 'trim|required|alpha|min_length[3]|max_length[30]|xss_clean');
    $this->form_validation->set_rules('email_id', 'Email ID', 'trim|required|valid_email|is_unique[email_id]');
    $this->form_validation->set_rules('user_password', 'Password', 'trim|required|matches[confirm_password]|md5');
    $this->form_validation->set_rules('confirm_password', 'Confirm Password', 'trim|required|md5');

    //validate form input

    if ($this->form_validation->run() == FALSE) {
        // fails
        $data = array();
        $data['title'] = 'Registration | Admin Panel';
        $data['header_content'] = $this->load->view('adminEntry/header_content', '', true);
        $data['footer_content'] = $this->load->view('adminEntry/footer_content', '', true);

        $this->load->view('adminentry/user_registration', $data);
    } else {

        //insert the user registration details into database

        echo '<pre>';
        print_r($data);
        exit();

        $data = array(
            'user_name' => $this->input->post('user_name'),
            'email_id' => $this->input->post('email_id'),
            'password' => $this->input->post('user_password'),

        );



        if ($this->user_model->insertuser($data)) {

            if ($this->user_model->sendEmail($this->input->post('email_id'))) {
                // successfully sent mail
                $this->session->set_flashdata('msg', '<div class="alert alert-success text-center">You are Successfully Registered! Please confirm the mail sent to your Email-ID!!!</div>');
                redirect('user_registration');
            } else {
                // error
                $this->session->set_flashdata('msg', '<div class="alert alert-danger text-center">Oops! Error.  Please try again later!!!</div>');
                redirect('user_registration');
            }
        } else {
            // error
            $this->session->set_flashdata('msg', '<div class="alert alert-danger text-center">Oops! Error.  Please try again later!!!</div>');
            redirect('user_registration');
        }
    }
}

User Model:

class User_model extends CI_Model {

//insert into user table
function insertUser($data) {

    return $this->db->insert('user', $data);
}

function sendEmail($to_email) {
    $from_email = 'sample@mail.com'; //change this to yours
    $subject = 'Verify Your Email Address';
    $message = 'Dear User,<br /><br />Please click on the below activation link to verify your email address.<br /><br /> http://example.com/user/verify/' . md5($to_email) . '<br /><br /><br />Thanks<br />Mydomain Team';

    //configure email settings
    $config['protocol'] = 'smtp';
    $config['smtp_host'] = 'ssl://smtp.example.com'; //smtp host name
    $config['smtp_port'] = '465'; //smtp port number
    $config['smtp_user'] = $from_email;
    $config['smtp_pass'] = 'examplepass'; //$from_email password
    $config['mailtype'] = 'html';
    $config['charset'] = 'iso-8859-1';
    $config['wordwrap'] = TRUE;
    $config['newline'] = "\r\n"; //use double quotes
    $this->email->initialize($config);

    //send mail
    $this->email->from($from_email, 'example.com');
    $this->email->to($to_email);
    $this->email->subject($subject);
    $this->email->message($message);
    return $this->email->send();
}

//activate user account
function verifyEmailID($key) {
    $data = array('approval_status' => 1);
    $this->db->where('md5(email_id)', $key);
    return $this->db->update('user', $data);
}

}

View Page:

<div class="container">
        <div class="full-content-center animated fadeInDownBig">
            <p class="text-center"><a href="#"><img src="<?php echo base_url(); ?>adminAssets/img/login-logo.png" alt="Logo"></a></p>
            <div class="login-wrap">
                <div class="row">
                    <div class="col-sm-6">
                        <?php echo $this->session->flashdata('verify_msg'); ?>
                    </div>
                </div>
            </div>
            <div class="login-block">
                <?php
                $attributes = array("name" => "registrationform");
                echo form_open("user_registration/index", $attributes);
                ?>
                <form role="form" method="post" action="<?php echo base_url(); ?>user_registration" enctype="multipart/form-data>">
                    <div class="form-group login-input">
                        <i class="fa fa-user overlay"></i>
                        <input type="text" name="user_name" value="<?php echo set_value('user_name'); ?>" class="form-control text-input" placeholder="Name">
                        <span class="text-danger"><?php echo form_error('user_name'); ?></span>
                    </div>
                    <div class="form-group login-input">
                        <i class="fa fa-envelope overlay"></i>
                        <input type="text" name="email_id" value="<?php echo set_value('email_id'); ?>" class="form-control text-input" placeholder="E-mail">
                        <span class="text-danger"><?php echo form_error('email_id'); ?></span>
                    </div>
                    <div class="form-group login-input">
                        <i class="fa fa-key overlay"></i>
                        <input type="password" name="user_password" value="<?php echo set_value('user_password'); ?>" class="form-control text-input" placeholder="Password" id="txtNewPassword">
                        <span class="text-danger"><?php echo form_error('user_password'); ?></span>
                    </div>

                    <div class="form-group login-input">
                        <i class="fa fa-key overlay"></i>
                        <input type="password" name="confirm_password" value="<?php echo set_value('confirm_password'); ?>" class="form-control text-input" placeholder="Confirm Password" id="txtConfirmPassword" onChange="isPasswordMatch();" >
                    </div>
                    <div class="form-group login-input" id="divCheckPassword"></div>

                    <div class="row">
                        <div class="col-sm-12">
                            <button type="submit" name="submit" class="btn btn-default btn-block">Register</button>
                        </div>
                    </div>
                    <?php echo form_close(); ?>
                    <?php echo $this->session->flashdata('msg'); ?>
                </form>

            </div>
        </div>

    </div>
</div>

Here is my error:

Error Number: 1146

Table 'counterpressing.email_id' doesn't exist

SELECT * FROM email_id WHERE email_id = 'shakil@gmail.com' LIMIT 1

Filename: D:/Xampp/htdocs/ffbdhub.com/system/database/DB_driver.php

Line Number: 691


Solution

  • Not sure if this has been brought up, but your validation rule isn't quite right... Your "is_unique()" requires the

    table and field, so you have the field name which is email_id, but your table name is nowhere to be seen.

    So whatever your table name is for your table containing email_id that you are wanting to check against, you need to change this...

    $this->form_validation->set_rules('email_id', 'Email ID', 'trim|required|valid_email|is_unique[email_id]');
    

    To this

    $this->form_validation->set_rules('email_id', 'Email ID', 'trim|required|valid_email|is_unique[table_name.email_id]');
    

    In simpler terms, you need to have is_unique[table_name.email_id] , where table_name is the name of the table that contains the email_id you are testing for.