I am learning codeigniter
when i create rule for validate dropdown box but its not given me error message

View File

<?php 
    $option = array('0' =>'[ Please select an Experience Level ]',
            '1' => 'Without experience',
            '2' => 'Internship',
            '3' => 'Less than one year',
            '4' => 'One year',
            '5' => 'Two years',);

        $shirts_on_sale = array('small','0');
    echo form_dropdown('RequiredEducationLevel',$option,'0');
?>
<?php echo form_error('RequiredEducationLevel','<span style="color:red;float:none;">','</span>');?>

Controller File

class Employee_Cont extends CI_Controller {

    public function __construct(){
        parent::__construct();
        $this->load->helper(array('form','url'));
        $this->load->helper('html');
        $this->load->library('form_validation');
    }

public function register()
{

    $this->form_validation->set_rules('RequiredEducationLevel','Required Education Level','required|callback_check_default');
    $this->form_validation->set_message('check_default', 'You need to select something other than the default');

    if($this->form_validation->run() == FALSE)
    {
            $this->load->view('header/employee_register_header');
            $this->load->view('employee_registration');
            $this->load->view('footer/employee_footer');
    }

    function check_default($post_string)
    {
        return $post_string == '0' ? false : true;
    }   

}

}

Can you suggestion me what is actual problem.

Thank You

Member Avatar for iamthwee

Will post up a full example in a bit.

Member Avatar for iamthwee

view file

<!-- use form open and point url to controller -->
<?php echo form_open('agency/form_validate'); ?>

<li id="AgencyType" style="z-index: 93;">
        <label class="mandatory" for="ExternalAgency">Are you an employment agency?</label>

        <ul>
            <li style="z-index: 92;">
                <?php 
                    $data = array(
                            'value'=>'ExternalAgency',
                            'name'=>'ExternalAgency',
                    );
                    echo form_radio($data);?>

                <label for="ExternalAgency">No, we recruit directly.</label>
            </li>

            <li style="z-index: 91;">
                <?php 
                    $data = array('value'=>'HeadhuntingAgency',
                                'name'=>'ExternalAgency');
                    echo form_radio($data); ?>

                <label for="HeadhuntingAgency">Yes, we are a headhunting/recruitment agency.</label>
            </li>

            <li style="z-index: 90;">

                <?php $data = array('value'=>'TemporaryAgency',
                            'name'=>'ExternalAgency'); 
                echo form_radio($data); ?>

                <label for="TemporaryAgency">Yes, we are a temporary staffing agency.</label>
            </li>

            <?php echo form_error('ExternalAgency','<span style="color:red;float:none;">','</span>'); ?>

    </ul>

    <!-- make sure to have a button submit and close form -->
    <?php echo form_submit('ok', 'ok'); ?>
    <?php echo form_close(); ?>

controller

public function form_validate()
    {

        echo $this->input->post('ExternalAgency');

        $this->form_validation->set_rules('ExternalAgency', 'Agency', 'required');

        //if input is incorrect
        if ($this->form_validation->run() == FALSE)
        {
            echo validation_errors();
        }
        else
        {
            //input is good so do calculations
        }
    }
Member Avatar for iamthwee

Important bits...

Make sure in your view to echo the path to the controller eg.

echo form_open('name_of_controller/form_validate');

I changed the radio array to include a value option as this is necessary.

I added a button and used a form_close.

It should be working now.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.