【问题标题】:my form validation is not working properly for select box in codeigniter我的表单验证不适用于 codeigniter 中的选择框
【发布时间】:2015-10-10 12:23:57
【问题描述】:

我正在 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>

城市: 选择城市 city_id?>">city_name?>

【问题讨论】:

    标签: php forms codeigniter validation drop-down-menu


    【解决方案1】:

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

    留言:

        $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'); ?>" />
    

    你的小错误: callback_select_validate callback_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');
    

    希望对你有帮助:)

    【讨论】:

    • 您的第二个解决方案正在运行......但第一个解决方案仍然显示错误消息,例如“无法访问与您的字段名称指定相对应的错误消息。(select_validate)”
    • 当你给我解决消息它不起作用的问题时。如您所示,我更改了 set_message 字段。你看我对控制器中的两条消息都使用了两个单独的函数。并感谢您提供第二个解决方案。
    • 不仅是第二个问题:对于空的其他字段,此消息解决方案不起作用。
    • 好的,您在其他函数中定义了 set_message(即 select_validate 和 select_validate1)。但是在定义所有规则之后尝试定义它。
    • 查看我编辑的代码并删除这两个函数(即 select_validate 和 select_validate1)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-10-24
    • 2013-11-09
    • 2017-05-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多