I have expanded the form_validation library with a simple function that ensures that the dropdown is not selected by default. Hope this helps.
applications / libraries / MY_Form_validation.php
<?php if (!defined('BASEPATH')) exit('No direct script access allowed.'); class MY_Form_validation extends CI_Form_validation { function __construct() { parent::__construct(); $this->CI->lang->load('MY_form_validation'); } function require_dropdown($str, $string_to_compare) { return ($str == $string_to_compare) ? FALSE : TRUE; } }
application / language / English / MY_Form_validation_lang.php
$lang['require_dropdown'] = 'The %s field must have an item selected.';
How to use:
1) Add your form to the drop-down list:
<select name="business_id"> <option value="select">Select Business</option> More options... </select>
2) Create a validation rule. You may be able to set the value to 0 and use require_dropdown [0], but I have never tried this.
$this->form_validation->set_rules('business_id', 'Business', 'require_dropdown[select]');
3) Set your own message: (or skip this step and use it in the language file.)
$this->form_validation->set_message('business_id', 'You must select a business');
zechdc
source share