baig772
baig772

Reputation: 3488

Codeigniter- How to preserve form values if error on submit

I have the form field like

<input type="text" class="form-control" name="postal_code" id="form_control_1" value="<?php if($user->postal_code   === NULL) { echo set_value('postal_code');} else { echo $user->postal_code;} ?>">
<label for="form_control_1">Postal Code</label>
<span class="help-block">Postal Code is required</span>

and my function is

public function postProfile() {
    if ($_POST) {


        $this->form_validation->set_rules('postal_code', 'Postal Code', 'required');
        $this->form_validation->set_rules('gender', 'Gender', 'required');
        $this->form_validation->set_rules('country', 'Country', 'required');
        $this->form_validation->set_rules('month', 'Month', 'required');
        $this->form_validation->set_rules('day', 'Day', 'required');
        $this->form_validation->set_rules('year', 'Year', 'required');
        $this->form_validation->set_rules('mobile_number', 'Mobile Number', 'required');


        if ($this->form_validation->run() === FALSE) {

            //$this->session->set_flashdata('err', validation_errors());
            $this->session->set_flashdata('email_err', form_error('email'));
            $this->session->set_flashdata('postal_code_err', form_error('postal_code'));
            $this->session->set_flashdata('gender_err', form_error('gender'));
            $this->session->set_flashdata('country_err', form_error('country'));
            $this->session->set_flashdata('day_err', form_error('day'));
            $this->session->set_flashdata('year_err', form_error('year'));
            $this->session->set_flashdata('month_err', form_error('month'));
            $this->session->set_flashdata('mobile_err', form_error('mobile_number'));
            redirect('profile/' . $this->session->userdata['front']['id']);
        } else {

            $dob = $this->input->post('month') . '/' . $this->input->post('day') . '/' . $this->input->post('year');
            $userData = array(
                'email' => $this->input->post('email'),
                'date_of_birth' => $dob,
                'gender' => $this->input->post('gender'),
                'country' => $this->input->post('country'),
                'mobile_number' => $this->input->post('mobile_number'),
                'postal_code' => $this->input->post('postal_code'),
            );


            $updateUser = $this->user->updateUser($this->session->userdata['front']['id'], $userData);
            if ($updateUser) {
                $this->session->set_flashdata('err', '<div class="alert alert-success">Profile Updated Successfuly</div>');
                redirect('profile/' . $this->session->userdata['front']['id']);
            } else {
                echo "Un expected error ";
                exit;
            }
        }
    } else {
        $this->logout();
    }
}

but value is not shown for the postal code field in case of validation errors

Upvotes: 1

Views: 5107

Answers (2)

Nouman Nazir
Nouman Nazir

Reputation: 73

You can set these values in flashdata and redirect to your form. For example

if ($this->form_validation->run() == FALSE) {

        $data = array(
                'errors' => validation_errors(),
                'name' => $this->input->post('name')//check this
            );

        $this->session->set_flashdata($data);

        redirect('YOUR-URL-HERE');
    }

And in your form use value attribute of form input as given below

<input type="text" class="form-control" name="name" placeholder="Name" value="<?= $this->session->flashdata('name'); ?>">

Upvotes: 2

Rejoanul Alam
Rejoanul Alam

Reputation: 5398

When error occur then you need to render view instead of rediect().

if ($this->form_validation->run() == FALSE) {
    // no need to set_flashdata here for form_error()
    $this->load->view('view_form',$data);
    return;
}else{
  //success code
}

Now view file

<input type="text" class="form-control" name="postal_code" id="form_control_1" value="<?php echo set_value('postal_code');?>">
<?php echo form_error('postal_code','<p class="alert alert-danger">','</p>');?>

Now your form will repopulate previous data if any error occur. In the same time you will see error message under your postal_code field

Upvotes: 1

Related Questions