Special Offer - Get $90 Discount and Host Your Dream Web Site @ Dreamhost for 1 Year Click Here. Promo Code SLJ345
Back To Top
WordPress Web Application Development
Develop Powerfull Web Applications Using Cutting Edge WordPress Development Techniques

Codeigniter Select Box Validation Using Validation Library

on 2011/08/21 11:48 PM stored in: Uncategorized and tagged:

Introduction To Validating Select Box Using Codeigniter

This code snippet will explain you how to validate a select box (drop down box) using Codeigniter. Create a dropdown box in the view file and add a default option. Set the value of default option (Example 0 ). Then add a new rule called selected to validation library and use the rule inside your controllers to validate the select box. All the source codes and instructions are given below.

View File Code

 
                 

              

Controller Class Code

public function loadView($data='') {
                $data['message'] = $this->message;
                $this->load->view('test_view',$data);

}

public function validate_select() {

                $this->validation->set_error_delimiters('

', '

');

                // Validation rules
                $rules['selCountry']    = "selected[selCountry]";
                $this->validation->set_rules($rules);

                $fields['selCountry']           = "Country";
                $this->validation->set_fields($fields);

                if ($this->validation->run() == FALSE) {

                        $this->message = $this->validation->error_string;

                        $this->loadView();

                } else {
                        // Validation is done and process with the logic
                }
}

Validation Library Code

function selected($field){
                
                if ($field == '0')
                {
                        return FALSE;
                }
                
                return TRUE;
}

Validation Language Code

$lang['selected']      = "The %s field is required.";

Source Code

Download Code

One Response to “Codeigniter Select Box Validation Using Validation Library”

  1. Dicas Beleza Feminina Says:

    I am usually to blogging and i really recognize your content. The article has really peaks my interest. I’m going to bookmark your site and preserve checking for brand new information.

Leave a Reply

Follow Us On Facebook