【发布时间】:2014-02-27 07:27:26
【问题描述】:
这里是控制器
class Form extends CI_Controller {
function index()
{
$this->load->helper(array('form', 'url'));
$this->load->library('form_validation');
$config = array(
array(
'field' => 'username',
'label' => 'Username',
'rules' => 'trim|required|min_length[5]|max_length[12]|is_unique[users.username]|xss_clean'
),
array(
'field' => 'password',
'label' => 'Password',
'rules' => 'trim|required|matches[passconf]|md5'
),
array(
'field' => 'passconf',
'label' => 'Password Confirmation',
'rules' => 'trim|required'
),
array(
'field' => 'email',
'label' => 'Email',
'rules' => 'trim|required|valid_email|is_unique[users.email]'
)
);
$this->form_validation->set_rules($config);
if ($this->form_validation->run() == FALSE)
{
$this->load->view('myform');
}
else
{
$this->load->view('formsuccess');
}
}
}
?>
这里是视图(myform.php,formsuccess.php)
myform.php
<html>
<head>
<title>My Form</title>
</head>
<body>
<?php echo validation_errors(); ?>
<?php echo form_open('form'); ?>
<h5>Username</h5>
<input type="text" name="username" value="" size="50" />
<h5>Password</h5>
<input type="text" name="password" value="" size="50" />
<h5>Password Confirm</h5>
<input type="text" name="passconf" value="" size="50" />
<h5>Email Address</h5>
<input type="text" name="email" value="" size="50" />
<div><input type="submit" value="Submit" /></div>
</form>
</body>
</html>
formsuccess.php
<html>
<head>
<title>My Form</title>
</head>
<body>
<h3>Your form was successfully submitted!</h3>
<p><?php echo anchor('form', 'Try it again!'); ?></p>
</body>
</html>
运行后,如果用户名不符合要求,则验证有效,但当用户名符合要求时,验证无效并进入成功页面,它应该适用于表单上的所有字段,而不仅仅是用户名。我该怎么办?对不起我的英语不好
【问题讨论】:
-
你试过
$this->form_validation->run() === FALSE吗? -
不适合小伙子:(还有其他建议吗?
-
尝试删除 is_unique[users.username] 和 is_unique[users.email],看看是否有效。
-
我认为 CI 验证没有任何问题。正如@Andrew 提到的,您在用户表中有用户名和电子邮件作为字段吗?
-
就是这样!有用!谢谢你的帮助
标签: php forms codeigniter validation