在codeigniter回调函数中显示flashdata消息

时间:2014-05-19 18:09:08

标签: php codeigniter

我有这个注册函数和相关的回调来检查数据库中是否已存在用户名。它完美无缺,但我遇到flashdata消息的问题

以下是代码:

/*
* Checks registration
*/
public function verify(){

$this->form_validation->set_rules('nome', 'Nome', 'trim|required|min_length[2]|xss_clean');
$this->form_validation->set_rules('cognome', 'Cognome', 'trim|required|min_length[2]|xss_clean');
$this->form_validation->set_rules('username', 'Username', 'trim|required|min_length[4]|xss_clean|callback_check_username');
$this->form_validation->set_rules('email', 'Email', 'trim|required|valid_email');
$this->form_validation->set_rules('password', 'Password', 'trim|required|min_length[4]|max_length[32]');
$this->form_validation->set_rules('password_conf', 'Password Confirmation', 'trim|required|matches[password]');

if($this->form_validation->run() == FALSE)
{
    $this->session->set_flashdata('error_reg', 'Something goes wrong, please check your registration form');    
    redirect('registration');   
}
else
{
    $this->registration_m->add_user();
    $this->session->set_flashdata('success_reg', 'Registration Successful!');
    redirect('registration');
}   
}


public function check_username($username){

$result = $this->registration_m->check_username_m($username);
if ($result) {
    $this->session->set_flashdata('username', 'Username già in uso, riprovare.');    
    return false;
} else {
    return true;
}
}

正如您所看到的,函数verify()上有一个重定向,并且在出现错误时正确显示之前的flashdata。

我的问题是:有没有办法在不改变这段代码的逻辑的情况下显示(如果有错误)回调函数中的flashdata? 希望我一点都不清楚,欢呼。

1 个答案:

答案 0 :(得分:0)

修改您的代码,如下所示

 public function check_username($username){    
   $result = $this->registration_m->check_username_m($username);
   if ($result) {
      $this->form_validation->set_message('check_username', 'The %s field can not be the empty');
      return false;
   } else {
      return true;
   }
  }

查看代码差异

 $this->session->set_flashdata('username', 'Username già in uso, riprovare.');    

 $this->form_validation->set_message('check_username', 'The %s field can not be the empty');