【问题标题】:Prevent form to display default values when validation fails codeigniter验证失败时防止表单显示默认值
【发布时间】:2014-03-26 11:12:14
【问题描述】:
public function edit_profile(){

        $logged_in_user = $this->session->userdata('logged_in');
        $data['images_url'] = $this->config->item('images_url');
        $data['cdn_url'] = $this->config->item('cdn_url');
        $data['reminder'] = $this->config->item('reminder');
        $data['current_module'] = "ess";
        $data['user_details'] = $this->ess->get_user_details($logged_in_user['user_id']);
        $this->load->view("header", $data);
        $this->load->view("leftbar", $data);
        $this->load->view("ess/ess_edit_profile");
        $this->load->view("footer", $data);

           if($this->input->post('submit')){
            $post_data = $this->input->post();
            $this->form_validation->set_rules('full_name','Full Name','required|trim|xss_clean');
            $this->form_validation->set_rules('email','Email','required|trim|valid_email');
            $this->form_validation->set_rules('blood_group','Blood Group','max_length[2]');
            $this->form_validation->set_rules('phone','Phone','required|trim|xss_clean');
            $this->form_validation->set_rules('address','Address','required|xss_clean|max_length[100]');
            $this->form_validation->set_rules('designation','Designation','required|xss_clean');
            $this->form_validation->set_rules('emergency_name','Emergency Name','required|xss_clean');
            $this->form_validation->set_rules('emergency_number','Emergency Number','required|xss_clean');
            $this->form_validation->set_rules('next_of_kin','Next of Kin','trim|xss_clean');

            if($this->form_validation->run() === true){
                // update data
            } else {
                $this->load->view("ess/ess_edit_profile");
            }



}
}

上面是我的控制器的功能,它在输入字段中加载一个编辑配置文件视图,其中包含数据库填充的值。现在,当表单在编辑后提交时,它会进入edit_profile() 函数中的验证过程。我故意运行错误的验证功能以进行检查。现在,如果验证运行 === false 我再次加载了ess_edit_profile 视图,但验证消息没有出现。表单字段也填充了我通过set_value() 函数设置的默认值

以下是我如何使用数据库数据显示表单字段

 <td>
                    <label for="full_name">Full Name</label>
                    <input style="width: 300px;" type="text" name="full_name" id="full_name" value="<?php echo set_value('full_name',$user_details[0]->ui_full_name); ?>" />
                    <?php echo form_error('full_name', '<span class="alert">', '</span>'); ?>
                </td>

【问题讨论】:

    标签: php codeigniter


    【解决方案1】:

    试试这个删除 else 部分然后试试

      if($this->form_validation->run() === true){
                    // update data
                } 
       $this->load->view("ess/ess_edit_profile");
    

    【讨论】:

    • 它有什么不同?
    【解决方案2】:

    请使用set_value函数设置元素值

    这样

    <input type="text" name="name" value="<?php echo set_value("name", $database_name)?>"/>
    

    在控制器中

    $this->form_validation->set_rules('name','Edit name','trim|xss_clean|required');
    

    如果不需要该值,则还要设置验证规则trim

    $this->form_validation->set_rules('name','Edit name','trim');
    

    【讨论】:

    • @Grish 我已经更新了我的问题。我已经在做这个请检查
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-06-20
    • 1970-01-01
    • 1970-01-01
    • 2018-08-01
    • 1970-01-01
    • 2012-11-09
    • 2015-04-17
    相关资源
    最近更新 更多