【问题标题】:Codeigniter- How to preserve form values if error on submitCodeigniter-如果提交时出错,如何保留表单值
【发布时间】:2016-05-14 23:11:55
【问题描述】:

我有这样的表单域

<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>

我的功能是

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();
    }
}

但在验证错误的情况下不会显示邮政编码字段的值

【问题讨论】:

标签: php codeigniter validation


【解决方案1】:

当发生错误时,您需要渲染视图而不是 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
}

现在查看文件

<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>');?>

现在,如果发生任何错误,您的表单将重新填充以前的数据。同时,您会在 postal_code 字段下看到错误消息

【讨论】:

  • 只是一个带有if ($this-&gt;form_validation-&gt;run() === FALSE) { 的注释,它应该只有两个== 而不是三个===
【解决方案2】:

您可以在 flashdata 中设置这些值并重定向到您的表单。例如

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

并且在您的表单中使用表单输入的 value 属性,如下所示

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

【讨论】:

    猜你喜欢
    • 2013-07-05
    • 2021-01-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-12-01
    • 1970-01-01
    相关资源
    最近更新 更多