Form Validation in Codeigniter

application/config/form_validation.php


<?php

$config = array(
        'register' => array(
                array(
                        'field' => 'name',
                        'label' => 'Name',
                        'rules' => 'required'
                ),
                array(
                        'field' => 'zipcode',
                        'label' => 'Zipcode',
                        'rules' => 'required'
                ),
                array(
                        'field' => 'email',
                        'label' => 'Email',
                        'rules' => 'required|callback_check_email_exists'
                ),
                array(
                        'field' => 'username',
                        'label' => 'Username',
                        'rules' => 'required|callback_check_username_exists'
                ),
                array(
                        'field' => 'password',
                        'label' => 'Password',
                        'rules' => 'required'
                ),
                array(
                        'field' => 'password2',
                        'label' => 'Password Confirmation',
                        'rules' => 'required|matches[password]'
                ),
                array(
                        'field' => 'captcha',
                        'label' => 'Captcha',
                        'rules' => 'required|callback_check_validate_captcha'
                )
             
        ),
        'login' => array(
                        array(
                        'field' => 'username',
                        'label' => 'Username',
                        'rules' => 'required'
                ),
                array(
                        'field' => 'password',
                        'label' => 'Password',
                        'rules' => 'required'
                ),

                        )
        );

?>


in controller file check validation as below :


if($this->form_validation->run('register')){

//statements
}


Inside Controller :

// Check if email exists
public function check_email_exists($email){
$this->form_validation->set_message('check_email_exists', 'That email is taken. Please choose a different one');
if($this->user_model->check_email_exists($email)){
return true;
} else {
return false;
}
}


// Check if username exists
public function check_username_exists($username){

$this->form_validation->set_message('check_username_exists', 'That username is taken. Please choose a different one');
if($this->user_model->check_username_exists($username)){
return true;
} else {
return false;
}
}


//check valid captcha

public function check_validate_captcha($captcha){
if($captcha != $this->session->userdata('scaptcha')){
$this->form_validation->set_message('check_validate_captcha','Please enter valid Captch');
return false;
}else{
return true;
}

}

Inside Model :


// Check username exists
public function check_username_exists($username){
$query = $this->db->get_where('users', array('username' => $username));
if(empty($query->row_array())){
return true;
} else {
return false;
}
}

// Check email exists
public function check_email_exists($email){
$query = $this->db->get_where('users', array('email' => $email));
if(empty($query->row_array())){
return true;
} else {
return false;
}

}

Comments

Popular posts from this blog

Set Base URL in config file in Codeigniter

Form Validation Using Javascript

Image Display Inside Form