Codeigniter表单验证将不会运行

时间:2012-06-22 11:26:23

标签: codeigniter validation activation

关于表单验证似乎有很多问题,但没有人回答我的问题。

除非我遗漏了一些明显的东西,否则我的表单验证将无法运行,但只在某个页面上,所有其他页面都可以。

以下是问题代码:

public function activate($code = '')
    {
        // This function lets a user activate their account with the code or link they recieved in an email
        if($code == '' || isset($_POST[''])){
            $form = '';
            $this->load->library('form_validation');
            $this->load->helper('form');

            if ($this->form_validation->run() == FALSE){
                // No code, so display a box for them to enter it manually
                $this->form_validation->set_rules('activation_code', 'Activation Code', 'trim|required|xss_clean|integer');

                $form .= validation_errors();
                $form .= form_open_multipart('user/activate', array('class' => 'formee'));

                $form .= form_label('Enter your activation code below and click \'Activate\' to start using your account. If you need a hand please contact live chat.', 'activation_code');
                $form .= form_input(array('name' => 'activation_code'));

                $data = array(
                    'name'        => 'submit',
                    'value'       => 'Activate',
                    'class'       => 'right',
                    'style'       => 'margin-top:10px;',
                );

                $form .= form_submit($data);
                $form .= form_close();
            }else{
                $form .= "form run";
            }
        }else{
            // Code recieved through the GET or POST variable XSS clean it and activate the account
        }

        $data = array(
            'title' => $this->lang->line('activate_title'),
            'links' => $this->gen_login->generate_links(),
            'content' => $form
        );
        $this->parser->parse('beer_template', $data);
    }

您可以在此处查看页面:http://77.96.119.180/beer/user/activate

您可以在此处查看表单验证:http://77.96.119.180/beer/user/register

有人可以帮忙吗?

1 个答案:

答案 0 :(得分:3)

您在运行后调用set_rules:

if ($this->form_validation->run() == FALSE){
  // No code, so display a box for them to enter it manually
  $this->form_validation->set_rules('activation_code', 'Activation Code', 'trim|required|xss_clean|integer');

但应该在之前:

$this->form_validation->set_rules('activation_code', 'Activation Code', 'trim|required|xss_clean|integer');
if ($this->form_validation->run() == FALSE){
}