Hi Geeks,
Today i am sharing you the Custom form validation example in codeigniter. Suppose we have a custom requirement to validate the data with our own custom rules like email already exist in database, So for this type on need can be resolve only with custom callback functions .these are the following steps to create a custom validation for any form.
Step 1: Form Layout form.php in views
<?php echo form_open_multipart('',array('name' => 'registration_form', 'id' => 'registration_form', 'class' => 'form-horizontal')); ?> <div class="item form-group"> <label class="control-label col-md-3 col-sm-3 col-xs-12">First Name</label> <div class="col-md-6 col-sm-6 col-xs-12"> <input type="text" id="f_name" name="f_name" value="<?php set_value('f_name'); ?>" class="form-control" /> <span class="required-server"><?php echo form_error('f_name'); ?> </span> </div> </div> <div class="item form-group"> <label class="control-label col-md-3 col-sm-3 col-xs-12">Last Name</label> <div class="col-md-6 col-sm-6 col-xs-12"> <input type="text" id="l_name" name="l_name" value="<?php set_value('l_name'); ?>" class="form-control" /> <span class="required-server"><?php echo form_error('l_name'); ?> </span> </div> </div> <div class="item form-group"> <label class="control-label col-md-3 col-sm-3 col-xs-12">Email</label> <div class="col-md-6 col-sm-6 col-xs-12"> <input type="text" id="user_email" name="user_email" value="<?php set_value('user_email'); ?>" class="form-control" /> <span class="required-server"><?php echo form_error('user_email'); ?> </span> </div> </div> <div class="item form-group"> <label class="control-label col-md-3 col-sm-3 col-xs-12">Password</label> <div class="col-md-6 col-sm-6 col-xs-12"> <input type="password" id="user_password" name="user_password" value="<?php set_value('user_password'); ?>" class="form-control" /> <span class="required-server"><?php echo form_error('user_password'); ?> </span> </div> </div> <?php echo form_close(); ?>
Now , We have 4 fields in this form –
first name,last name, email and password
for validate these fields codeigniter already provided rules in form_validation library:
Some of the rules are following listed:
valid_email,required,valid_email,valid_url,min_length[5]|max_length[12] etc.
But Now we need to detect that email already registered in Database ,So we need to create a callback function for validating the email already check in database.
Note:Codeigniter itself providing rule for ‘value already exists in database’ but here our purpose is to demonstrate the custom form validation in codeigniter thats why we are assuming that this need a custom form validation.
Step 2. Create a callback to match the field’s value in database
public function check_email_exists($email){ $where_array = array( user_email' => $email ); $this->db->where($where_array); $switch = $this->db->count_all_results("tbl_user"); if ($switch != NULL){ $this->form_validation->set_message('check_email_exists', 'The %s field value already exist, please try another.'); return false; }else{ return true; } }
Step 3. Load Form validation library in controller’s Action or Constructor method as following:
$this->load->library('form_validation');
Step 4. Now pass the array of validation rules for the form in add method of user controller as following:
public function add() { $this->load->library('form_validation'); $config = array( array( 'field' => 'f_nme', 'label' => 'First Name', 'rules' => 'trim|required|xss_clean' ), array( 'field' => 'l_name', 'label' => 'Last Name', 'rules' => 'trim|required|xss_clean' ), array( 'field' => 'user_email', 'label' => 'Email Address', 'rules' => 'trim|required|xss_clean|callback_check_email_exists' //here we have added the callback which we have created by appending callback_ to its method name ), array( 'field' => 'user_password', 'label' => 'Password', 'rules' => 'trim|required|xss_clean' ) ); $this->form_validation->set_rules($config); //pass the rules array here if ($this->form_validation->run() == FALSE) { //by default initial load condition $this->load->view('form'); } else{ //controll comes here if form submitted successfully //Now add or update the data // $save_data = array( 'f_name' =>$this->input->post('f_name'), 'l_name' => $this->input->post('l_name'), 'user_email=> $this->input->post('user_email'), 'user_password' => $this->input->post('user_password') ); $this->db->insert('tbl_user',$save_data);//add data to tabke user } } ?>
Please comment here if you have any query regarding to this form validation snippet or not cleared anything.