[英]How do I echo a form error in a else statement in my controller in code-igniter?
我有所有不同的组合,它们可以按应有的方式在代码点火器中为我的登录表单提交错误的表单。
**编辑-这似乎是最合乎逻辑的形式,因为其他建议正在加载双重视图,或者与我的结构没有凝聚力。 除一个错误外,所有错误均正常工作。
当您单击“使用正确的电子邮件和乱码提交”时,不会出现任何错误,并且页面会重新加载,并自动填写输入的电子邮件,并且将密码字段设置回默认的“ PASSWORD”文本。
我一直坚持下去太久了,任何帮助将不胜感激。
function validate_credentials_login()
{
$this->load->library('session');
$this->load->helper(array('form','url'));
$this->load->model('user_model', 'um');
$this->load->library('encrypt');
$this->load->library('form_validation');
$this->form_validation->set_rules('email_login', 'Email', 'trim|required|valid_email');
$this->form_validation->set_rules('password_login', 'Password', 'trim|required');
if ( $this->form_validation->run() === TRUE )
{
$user = $this->um->validate_home_login(array('email' => $this->input->post('email_login')));
if ( $user )
{
if ( $user->password == $this->encrypt->sha1( $user->salt . $this->encrypt->sha1($this->input->post('password_login'))) && $user->email == $this->input->post('email_login') )
{
$this->session->set_userdata(array('email' => $this->input->post('email_login')));
redirect('account/edit');
}
else
{
$this->form_validation->run() == FALSE;
}
}
else
{
$this->form_validation->run() == FALSE;
}
}
$data['main_content'] = 'home/home_page';
$this->load->view('includes/templates/home_page_template', $data);
}
我要做的(和已经做的)只是在登录视图页面上使用validation_errors()
。 您可以这样做:
if ($user)
if ($success)
{
redirect(...);
}
}
$this->load->view('login_page_template', $data); // Not sure what your login view is named
并在登录视图中:
...
<?php echo validation_errors('<div class="errors_login">', '</div>'); ?>
...
如果您稍微更改逻辑,则可以摆脱else
,而只需在if
语句运行后加载登录视图。 如果用户成功登录,则无论如何都将在登录视图加载之前对其进行重定向。 这样,您就不必else
每一个if语句,并一遍又一遍地重复相同的代码。 让我知道这是否没有道理。
编辑:就像@ m4t1t0所说的那样,您不必将错误传递给视图,您只需echo validation_errors('<div class="errors_login">', '</div>');
无论您要在哪里显示错误。 我已经更新了示例以反映这一点。
如果要在每个表单项上方分别显示错误(例如,电子邮件不存在,密码错误等),则可以使用echo form_error('field_name');
在每个位置。
编辑2:我也注意到您正在使用sha1加密作为密码。 对于安全性较低的设置,这很好,但是我建议使用PHP的crypt()
函数,该函数默认使用Blowfish算法(如果可用)。 这是一个基本的实现:
// to make password hash
$password = crypt($this->input->post('password'));
// to check password hash
if (crypt($this->input->post('password'), $password_from_db) == $password_from_db)
{
// success
}
正如@Brendan所说,您不需要最后两个else语句,但是您甚至不需要将错误传递给视图! 您可以输入以下代码:
在控制器中:
$this->form_validation->set_rules('email_login', 'Email', 'trim|required|valid_email');
$this->form_validation->set_rules('password_login', 'Password', 'trim|required');
if ( $this->form_validation->run() === TRUE )
{
$user = $this->um->validate_home_login(array('email' => $this->input->post('email_login')));
if ( $user )
{
if ( $user->password == $this->encrypt->sha1( $user->salt . $this->encrypt->sha1($this->input->post('password_login'))) && $user->email == $this->input->post('email_login') )
{
$this->session->set_userdata(array('email' => $this->input->post('email_login')));
redirect('account/edit');
}
$data['main_content'] = 'home/home_page';
$this->load->view('includes/templates/home_page_template', $data);
}
并在视图中:
<?php echo validation_errors('<div class="error">', '</div>'); ?>
仅当验证表单时出错时才显示<div class =“ error”,否则将不显示任何内容。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.