Php 对于codeigniter中的选择框,我的表单验证无法正常工作

Php 对于codeigniter中的选择框,我的表单验证无法正常工作,php,forms,codeigniter,validation,drop-down-menu,Php,Forms,Codeigniter,Validation,Drop Down Menu,我在codeigniter中进行表单验证,我在控制器中为表单创建验证规则。我有两个选择框,我想验证这两个现在的问题是两个选择框显示相同的错误消息我想为每个选择框显示不同的消息 主要问题是错误消息将显示两个选择框的相同城市错误。。我想为不同的选择框显示不同的错误消息 还有一个问题::当我收到任何一个字段的验证错误消息时,整个表单将变为空。我想这样显示,当我得到任何字段的验证错误时,其他字段数据将在那里 这是我的控制器: public function addEmployeeController()

我在codeigniter中进行表单验证,我在控制器中为表单创建验证规则。我有两个选择框,我想验证这两个现在的问题是两个选择框显示相同的错误消息我想为每个选择框显示不同的消息

主要问题是错误消息将显示两个选择框的相同城市错误。。我想为不同的选择框显示不同的错误消息

还有一个问题::当我收到任何一个字段的验证错误消息时,整个表单将变为空。我想这样显示,当我得到任何字段的验证错误时,其他字段数据将在那里

这是我的控制器:

public function addEmployeeController()
{

    $this->load->library('form_validation');

    $abcd = $this->input->post('city_id');
    $abc = $this->input->post('desi_id');


    $this->form_validation->set_rules('emp_name', 'Name', 'trim|required');
    $this->form_validation->set_rules('emp_jdate', 'Joining Date', 'trim|required');
    $this->form_validation->set_rules('emp_addr', 'Address', 'trim|required');
    $this->form_validation->set_rules('emp_sal', 'Salary', 'trim|required');
    $this->form_validation->set_rules('emp_descr', 'Description', 'trim|required');
    $this->form_validation->set_rules('emp_mobile', 'Mobile Number', 'trim|required|min_length[10]');
    $this->form_validation->set_rules('city_id', 'City', 'trim|required|callback_select_validate');
    $this->form_validation->set_rules('desi_id', 'Designation', 'trim|required|callback_select_validate');



    if($this->form_validation->run() == FALSE)
    {
        $this->index();         
    }

    else
    {           


        if($query = $this->Emp_model->addEmployeeModel('$data'))
        {
            $data['main_content'] = 'signup_successful';
            $this->load->view('emp_view', $data);
        }
        else
        {
            $this->load->view('emp_view');          
        }
    }

}

public function select_validate($abcd)
{
        // 'none' is the first option that is default "-------Choose City-------"
        if($abcd == "none")
        {
                $this->form_validation->set_message('select_validate', 'Please Select Your City.');

                return false;
        } 
        else
            {
                // User picked something.
                return true;
            }
}

public function select_validate1($abc)
{
        // 'none' is the first option that is default "-------Choose City-------"
        if($abc = "none")
        {
                $this->form_validation->set_message('select_validate', 'Please Select Your Designation.');
                return false;
        } 
        else
            {
                // User picked something.
                return true;
            }
}
在视图中,我有这样的选择框。当我在未选择选择框的情况下提交表单时,会显示错误消息“请选择您的城市” 对于两个选择框。我想展示不同的信息

<p>
    <lable for="desi_id">Designation:</lable><?php echo form_error('desi_id'); ?>

        <select name="desi_id">
        <option selected="selected" value="none">Select Post</option>   

        <?php foreach ($records as $row) { ?>

        <option value="<?php echo $row->desi_id ?>"><?php echo $row->post_name ?></option>
        <?php } ?>


        </select>

    </p>

任命:
选择职位

您好,您需要定义不同的消息:

对于消息:

    $this->form_validation->set_rules('emp_name', 'Name', 'trim|required');
    $this->form_validation->set_rules('emp_jdate', 'Joining Date', 'trim|required');
    $this->form_validation->set_rules('emp_addr', 'Address', 'trim|required');
    $this->form_validation->set_rules('emp_sal', 'Salary', 'trim|required');
    $this->form_validation->set_rules('emp_descr', 'Description', 'trim|required');
    $this->form_validation->set_rules('emp_mobile', 'Mobile Number', 'trim|required|min_length[10]');
    $this->form_validation->set_rules('city_id', 'City', 'trim|required|callback_select_validate');
    $this->form_validation->set_rules('desi_id', 'Designation', 'trim|required|callback_select_validate1');

    $this->form_validation->set_message('city_id', 'Your message');
    $this->form_validation->set_message('desi_id', 'Your other message');
对于空的其他字段: 您必须在所有文件中使用set_value('field_name')

<input type="text" name="city" value="<?php echo set_value('city'); ?>" />

希望这对您有所帮助:)

您的第二个解决方案正在发挥作用。。。但1st仍然显示一个错误,如“无法访问与字段名称指定相对应的错误消息。(选择验证)”,因为您为消息提供了解决方案,消息无效。我更改您显示的set_消息字段。你们看,我在控制器中为这两条消息使用了两个单独的函数。感谢第二个解决方案。不仅仅是第二个问题:对于空的其他字段,此消息解决方案不起作用。确定您在其他函数中定义了set_消息(即选择_验证并选择_验证1)。但是,在定义所有规则之后,请尝试定义它。请参阅我编辑的代码并删除这两个函数(即select_validate和select_validate1)
$this->form_validation->set_rules('city_id', 'City', 'trim|required|callback_select_validate');
$this->form_validation->set_rules('desi_id', 'Designation', 'trim|required|callback_select_validate1');