CodeIgniter Forums
If form validation fails - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forum-20.html)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forum-23.html)
+--- Thread: If form validation fails (/thread-56686.html)



If form validation fails - El Forum - 01-11-2013

[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?


If form validation fails - El Forum - 01-11-2013

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