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
September 1st, 2011 at 2:05 pm
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.