表单验证CI不适用于所有字段

时间:2014-02-27 07:27:27

标签: php forms codeigniter validation

这是控制器

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>

运行之后,如果用户名不符合要求,验证工作正常,但当用户名符合要求,验证不起作用并转到成功页面时,它应该适用于表单上的所有字段,而不仅仅是用户名。我该怎么办?抱歉我的英文不好

1 个答案:

答案 0 :(得分:0)

formigniter.org/app

我更喜欢使用它,它会自动添加验证。