Welcome Guest, Not a member yet? Register   Sign In
How can I persist invalid edit form data?
#1

I am working on a basic blog application with [b]Codeigniter 3.1.8[/b] and [b]Bootstrap 4[/b].

There is, among others an "Edit account information" form:



Code:
<?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(); ?>

It's corresponding [b]controller[/b] logic is this:



Code:
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'));
    }
}

The problem I have not been able to solve is related to the validation of this form: if I leave a [i]required filed empty[/i], and try to submit the form, the form is loaded, with the proper validation error message, only the invalid field is populated with the data from the database and so, valid data is displayed along with the error message, as seen in the form below:
[color=var(--blue-700)][Image: govab.jpg]

How can I solve this issue?
[/color]
Reply




Theme © iAndrew 2016 - Forum software by © MyBB