防止表单在验证失败时显示默认值


Prevent form to display default values when validation fails codeigniter

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。现在,如果验证运行=== 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>

尝试删除其他部分,然后尝试

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

请使用set_value函数设置元素值

Like this

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

In Controller

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

如果不需要这个值,那么也设置验证规则trim

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