在编码器回调函数中显示flashdata消息


Display flashdata message in codeigniter callback function

我有这个注册函数和相关的回调来检查用户名是否已经存在于数据库中。它工作得很完美,但我有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而不改变这段代码的逻辑?希望我讲得很清楚,谢谢。

按如下方式修改代码

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