Skip to content

CodeIgniter form dropdown Validation?

Last updated on January 17, 2018

This post is about CodeIgniter Dropdown Menu(all so know as a select box) validation.See the below code samples for better understanding.

Create View : application/views/welcome_message.php

$options = array('-1' => 'SELECT SHIRT', 'small' => 'Small Shirt', 'med' =>
    'Medium Shirt', 'large' => 'Large Shirt', 'xlarge' => 'Extra Large Shirt', );
echo form_open('welcome/index');
echo form_error('shirts');
echo form_dropdown('shirts', $options);
echo form_submit('submit', 'submit');
echo form_close();
?>

Create Controler file : appliction/controllers/welcome.php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');
 
class Welcome extends CI_Controller
{
 
    public function index()
    {
        $this->load->helper(array('form'));
        $this->load->library(array('form_validation'));
        $this->form_validation->set_rules('shirts', 'shirts', 'required|alpha_numeric');
        $this->form_validation->set_message('alpha_numeric',
            'You need to select something other than the default');
        if ($this->form_validation->run() == false) {
            $this->load->view('welcome_message');
        } else {
            print_r($this->input->post('shirts'));
        }
    }
}
 
/* End of file welcome.php */
/* Location: ./application/controllers/welcome.php */
0 0 votes
Article Rating
Subscribe
Notify of
guest

2 Comments
Most Voted
Newest Oldest
Inline Feedbacks
View all comments