3

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

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

C:\wamp\www\CodeIgniter\application\controllers\user.php 在第 92 行

这是相关的控制器代码:

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

PS:这是我在表单中显示值的方式:

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

1 回答 1

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

不过,如果您需要成功消息,您可以为此使用闪存数据。这里

于 2013-10-14T07:14:25.287 回答