Welcome Guest, Not a member yet? Register   Sign In
If form validation fails
#1

[eluser]Patrick Reck[/eluser]
Hello!

I want to redirect people to a certain page if the form validations fails. However, I can't quite figure out how.

If i redirect people at the REDIRECT HERE comment below, it also redirects when it loads up the form and causes a endless loop.

Code:
public function create() {
        
        $this->form_validation->set_rules('email_adress', 'E-mail', 'required|valid_email|is_unique[users.email_adress]');

        if ($this->form_validation->run() !== FALSE) {
             // PASSED
        } else {
            // REDIRECT HERE
        }
        
        $this->load->view('user_register_view');
    }

How can I achieve this?
#2

[eluser]PhilTem[/eluser]
I always have my controller first check if there was anything submitted in $_POST before I perform the form-validation. This way, the form-validation is not executed on the initial page loading. The code is something like this

Code:
if ( $this->input->post('name_of_submit_btn') )
{
  if ( $this->form_validation->run() === TRUE )
  {
    // Do whatever
  }
  else
  {
    // Set a flash message
  }
}

// Show form

In your case, you might also first wanna try to type-sensitive check for TRUE and for FALSE i.e.
Code:
if ( $this->form_validation->run() === TRUE )
{
  redirect();
  exit();
}




Theme © iAndrew 2016 - Forum software by © MyBB