Php 在codeigniter回调函数中显示flashdata消息
我使用了这个注册函数和相关的回调函数来检查数据库中是否已经存在用户名。这一切都很完美,但我对flashdata消息有问题 代码如下:Php 在codeigniter回调函数中显示flashdata消息,php,codeigniter,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->
/*
* 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');
你想显示
flashdata('error_reg')
内部的检查用户名
?我想在注册
页面上显示错误消息,在那里我在函数验证
中重定向。好的,但是注册是另一个控制器,路由的,对吗?然后,您可以在注册控制器上:$data['error']=$this->session->flashdata('error_reg')
和$this->load->view('page.php',$data)代码>在视图上,简单echo$error
;据我所知,没有。如果这已成为代码中的一种模式,并且确保您使用的是flashdata,则可以编写一个库来读取参数并直接从视图中调用它。会更干净,因为您不需要控制器来收集会话值和可重用。在回调中,您应该使用$this->form\u validation->set\u message()而不是$this->session->set\u flashdata()。这样,如果存在任何验证错误,form_验证本身将创建flashdata消息。