Question

There seems to be many questions about form validation but none answer my problem.

Unless I'm missing something obvious, my form validation will not run, but only on a certain page, all other pages are fine.

Here is the problem code:

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);
    }

You can see the page here: http://77.96.119.180/beer/user/activate

You can see form validation working here: http://77.96.119.180/beer/user/register

Can anyone help?

Was it helpful?

Solution

You call the set_rules after the run:

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');

But should be before:

$this->form_validation->set_rules('activation_code', 'Activation Code', 'trim|required|xss_clean|integer');
if ($this->form_validation->run() == FALSE){
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top