Codeigniter form_validation始终返回false

时间:2013-11-14 14:31:01

标签: php codeigniter validation

我尝试验证我的表单,但它总是返回FALSE。表单填充POST变量,验证配置已设置,但它不起作用。我尝试了一切,现在我坚持了下来。也许你的眼睛会发现错误。

这是我的控制器:

    public function new_pass()
{
    $code = mysql_real_escape_string($this->uri->segment(3));
    $this->load->model('forgot_password_model');

    if($this->forgot_password_model->check_code($code))
    {
        if($this->input->post('submit'))
        {
            $this->new_pass_validation();
        }
        else
        {
            $this->new_pass_form();
        }
    }
    else
    {
        redirect('welcome');
    }
}

private function new_pass_validation()
{
    $this->load->library('form_validation');

    var_dump($this->form_validation->run('forgot_password/new_pass'));
    var_dump($this->input->post());
    if ($this->form_validation->run() === FALSE)
    {
//          print_r('dump' . validation_errors());
        $this->new_pass_form();
    }
    else
    {

    }
}

config / form_validation:

$config = array(
     'forgot_password/new_pass' => array(
          array(
                array(
                    'field' => 'password',
                    'label' => lang('default_jelszo'),
                    'rules' => 'trim|required|min_length[5]|max_length[32]'
              ),
              array(
                    'field' => 'repassword',
                    'label' => lang('default_jelszo_megerosites'),
                    'rules' => 'trim|required|matches[password]|callback_password_hash'
              ),
          )
     ),
);

观点:

<section id="content">
    <a id="logo" href=""></a>
    <?=validation_errors(); ?>
    <div class="formwrapp">
        <form action="<?=site_url() . $this->uri->uri_string(); ?>" method="post">
            <h3>Forgot password</h3>
            <ul>
                <li>Pass</li>
                     <li><input type="password" value="" name="password" /></li>

                <li>Repass</li>
                     <li><input type="password" value="" name="repassword" /></li>

                <li>&nbsp;</li><li><input id="login" type="submit" value="Submit" name="submit" /></li>
            </ul>
        </form>
    </div>
</section>

2 个答案:

答案 0 :(得分:2)

您的配置文件中有一个不必要的数组,请尝试:

$config = array(
    'forgot_password/new_pass' => array(
        array(
            'field' => 'password',
            'label' => lang('default_jelszo'),
            'rules' => 'trim|required|min_length[5]|max_length[32]'
         ),
         array(
             'field' => 'repassword',
             'label' => lang('default_jelszo_megerosites'),
             'rules' => 'trim|required|matches[password]|callback_password_hash'
         )
     )
);

答案 1 :(得分:0)

我认为做这项工作的最好方法就是按照这个链接...即不使用配置类型,但如果你想使用配置,我想你已经忘记编写代码......

$this->form_validation->set_rules($config); 

http://ellislab.com/codeigniter%20/user-guide/libraries/form_validation.html