2013-10-14 27 views
3

在我看來,我想要做的就是清除表單字段,一旦用戶被成功註冊。一切工作正常這裏即用戶正在註冊,正在顯示出不同之處在於用戶成功的消息就是我想要做的是表單字段的明確的價值觀,我用這Codeigniter:重置表單值

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

C:\wamp\www\CodeIgniter\application\controllers\user.php on line 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(); 

P.S:這是怎麼了顯示形式的值:

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

回答

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

+0

+1不僅提供了正確的解決方案,而且還推薦了一種更好的方式:) –