【发布时间】:2017-02-07 12:26:23
【问题描述】:
我正在使用 codeigniter 3。我在表单中有一个电子邮件字段并在电子邮件上使用required|callback_is_email_exist rules。当我将电子邮件字段留空时,它显示回调消息而不是必需的消息。
我曾在 codeigniter 2 上工作过,它完美地显示“必需”消息,CI3 表单验证在使用回调时未按顺序执行规则。
以下是我的代码
查看:welcome_message
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Welcome to CodeIgniter</title>
</head>
<body>
<?php echo validation_errors(); ?>
<?php echo form_open('Welcome'); ?>
<h5>Username</h5>
<input type="text" name="email" value="<?php echo set_value('email'); ?>" size="50" />
<div><input type="submit" value="Submit" /></div>
</form>
</body>
</html>
控制器:welcome.php
public function index()
{
$this->load->helper(array('form', 'url'));
$this->load->library('form_validation');
$this->form_validation->set_rules('email', 'Email', 'required|callback_is_email_exist');
if ($this->form_validation->run() == FALSE)
{
$this->load->view('welcome_message');
}
else
{
echo 'success';
}
}
public function is_email_exist($str)
{
// code to check email exist in databse here
if (is_email_exist($str)
{
return TRUE;
}
else
{
$this->form_validation->set_message('is_email_exist', 'Email Does not exist');
return FALSE;
}
}
预期输出
需要填写电子邮件
Codeingiter 3 期望按顺序执行规则。如果一个成功然后下一个执行。在这种情况下,如果我将电子邮件字段留空,它会不必要地执行回调。因此它显示错误消息。它应该显示电子邮件字段是必需的而不是电子邮件不存在。
感谢您的反馈。
【问题讨论】:
标签: php codeigniter