如何在此 Codeigniter 3 应用程序中保留无效的编辑表单数据?

How can I persist invalid edit form data in this Codeigniter 3 application?

我正在使用 Codeigniter 3.1.8Bootstrap 4.[=13= 开发一个基本的博客应用程序]

其中有一个 "Edit account information" 形式:

<?php echo form_open(base_url('dashboard/users/update')); ?>

        <input type="hidden" name="id" id="uid" value="<?php echo $author->id; ?>">

        <div class="form-group <?php if(form_error('first_name')) echo 'has-error';?>">
          <input type="text" name="first_name" id="first_name" class="form-control" value="<?php echo $author->first_name;?>" placeholder="First name">
          <?php if(form_error('first_name')) echo form_error('first_name'); ?> 
        </div>

        <div class="form-group <?php if(form_error('last_name')) echo 'has-error';?>">
          <input type="text" name="last_name" id="last_name" class="form-control" value="<?php echo $author->last_name;?>" placeholder="Last name">
          <?php if(form_error('last_name')) echo form_error('last_name'); ?> 
        </div>

        <div class="form-group <?php if(form_error('email')) echo 'has-error';?>">
          <input type="text" name="email" id="email" class="form-control" value="<?php echo $author->email;?>" placeholder="Email">
          <?php if(form_error('email')) echo form_error('email'); ?> 
        </div>

        <div class="form-group <?php if(form_error('bio')) echo 'has-error';?>">
          <textarea name="bio" id="bio" cols="30" rows="5" class="form-control" placeholder="Add a short bio"><?php echo $author->bio; ?></textarea>
          <?php if(form_error('bio')) echo form_error('bio'); ?> 
        </div>

        <div class="form-group">
          <input type="submit" value="Save" class="btn btn-block btn-md btn-success">
        </div>

<?php echo form_close(); ?>

对应的controller逻辑是这样的:

public function edit($id) {
    // Only logged in users can edit user profiles
    if (!$this->session->userdata('is_logged_in')) {
        redirect('login');
    }

    $data = $this->Static_model->get_static_data();
    $data['pages'] = $this->Pages_model->get_pages();
    $data['categories'] = $this->Categories_model->get_categories();
    $data['author'] = $this->Usermodel->editAuthor($id);

    $this->load->view('partials/header', $data);
    $this->load->view('dashboard/edit-author');
    $this->load->view('partials/footer');

}

public function update() {
    // Only logged in users can edit user profiles
    if (!$this->session->userdata('is_logged_in')) {
        redirect('login');
    }

    $id = $this->input->post('id');

    $data = $this->Static_model->get_static_data();
    $data['pages'] = $this->Pages_model->get_pages();
    $data['categories'] = $this->Categories_model->get_categories();
    $data['author'] = $this->Usermodel->editAuthor($id);

    $this->form_validation->set_rules('first_name', 'First name', 'required');
    $this->form_validation->set_rules('last_name', 'Last name', 'required');
    $this->form_validation->set_rules('email', 'Email', 'required|trim|valid_email');

    $this->form_validation->set_error_delimiters('<p class="error-message">', '</p>');

    if($this->form_validation->run() === FALSE)
    {
        $this->load->view('partials/header', $data);
        $this->load->view('dashboard/edit-author');
        $this->load->view('partials/footer');

    } else
    {
        $this->Usermodel->update_user($id);
        $this->session->set_flashdata('user_updated', 'Your account details have been updated');
        redirect(base_url('/dashboard/manage-authors'));
    }
}

我未能解决的问题与此表单的验证有关:如果我将 必填字段留空,然后尝试提交表单,表单已加载,并带有正确的验证错误消息,只有无效字段会填充数据库中的数据,因此,有效数据会与错误消息一起显示,如下表所示:

但是,只要字段的当前值没有被无效替换,我希望它(字段值)来自数据库。

我该如何解决这个问题?

如果您想将提交的数据保留在输入值上,您可以像这样修改电子邮件代码:

<div class="form-group <?php if(form_error('email')) echo 'has-error';?>">
    <input type="text" name="email" id="email" class="form-control" value="<?php echo set_value('email', $author->email); ?>" placeholder="Email">
    <?php if(form_error('email')) echo form_error('email'); ?> 
</div>

这会将提交的数据设置为电子邮件值(如果可用),如果没有提交的电子邮件数据,则设置为来自数据库的电子邮件数据。