Codeigniter:重置表单值

时间:2013-10-14 06:22:03

标签: php codeigniter validation

在我看来,我想要做的是在用户成功注册后清除表单字段。一切正常,即用户正在注册,成功消息正在向用户显示,除了我想要做的是清楚表单字段的值,我正在使用此

// Clear the form validation field data, so that it doesn't show up in the forms
$this->form_validation->_field_data = array();

我添加后,CI一直给我这个错误:  致命错误:无法在

中访问受保护的属性CI_Form_validation :: $ _ field_data
  第92行的

C:\ wamp \ www \ CodeIgniter \ application \ controllers \ user.php

以下是相关控制器代码:

public function signup()
{
    // If the user is logged in, don't allow him to view this page.
    if (($this->_isLoggedIn()) === true) {
        $this->dashboard();
    }
    else
    {
        $data['page']       = 'signup';
        $data['heading']    = 'Register yourself';
        $data['message']    = $this->_regmsg;

        $this->load->library('form_validation');

        // $this->form_validation->set_rules('is_unique', 'Sorry! This %s has already been taken. Please chose a different one.');

        $this->form_validation->set_rules('username', 'Username', 'required|min_length[5]|max_length[12]|is_unique[users.username]|callback_valid_username');
        $this->form_validation->set_rules('password', 'Password', 'required|matches[passconf]');
        $this->form_validation->set_rules('passconf', 'Password Confirmation', 'required');
        $this->form_validation->set_rules('email', 'Email', 'required|valid_email|is_unique[users.email]');

        // run will return true if and only if we have applied some rule all the rules and all of them are satisfied
        if ($this->form_validation->run() == false) {

             $data['errors'] = isset($_POST['submit']) ? true : false;
            $data['success'] = false;

            $this->_load_signup_page($data);
        }
        else{
            if($this->users->register_user($_POST)){
                 $data['errors'] = false;
                $data['success'] = true;

                // Clear the form validation field data, so that it doesn't show up in the forms
                $this->form_validation->_field_data = array();

                $this->_load_signup_page($data);
            }
        }
    }
}

private _load_signup_page($data){
    $this->load->view('template/main_template_head');
    $this->load->view('template/blue_unit', $data);
    $this->load->view('signup', $data);
    $this->load->view('template/main_template_foot');
}

任何人都可以告诉我这条线的交易是什么?

$this->form_validation->_field_data = array();

P.S:以下是我在表单中显示值的方法:

<?php echo set_value('fieldname'); ?>

1 个答案:

答案 0 :(得分:4)

这意味着这是受保护的属性,您无法直接使用它。而不是这样,你可以像这样做

if($this->users->register_user($_POST)){
    $data['errors'] = false;
    $data['success'] = true;

    unset($_POST)

    $this->_load_signup_page($data);
}

不推荐这种方式。相反,如果您重定向到同一个控制器,表单将自动重置。

if($this->users->register_user($_POST)){
    $data['errors'] = false;
    $data['success'] = true;

    redirect('controllername/signup');
}

如果您需要成功消息,可以使用闪存数据。 Here