Codeigniter form validation: how to redirect to previous page if any validation error is detected?

I use the Codeigniter validation class to validate my form. Could you tell me how to redirect to the previous page from the controller if any validation error is detected?

In my controller:

if ($this->form_validation->run() == FALSE){ //**** Here is where I need to redirect } else { // code to send data to model... } 
+6
source share
4 answers

UPDATE

You want to publish the form, verify it, and then show the form again with validation errors if the validation fails, or show something completely different if the validation passes.

The best way to do this is to send the form back to yourself. Thus, the action of your form will be action="" . Thus, in your method, you can check if the form has been submitted and determine what to do there:

 // in my form method if ($this->input->post('submit')) // make sure your submit button has a value of submit { // the form was submitted, so validate it if ($this->form_validation->run() == FALSE) { $this->load->view('myform'); } else { $this->load->view('formsuccess'); } } else { // the form wasn't submitted, so we need to see the form $this->load->view('myform'); } 

OLD ANSWER

You can always pass the current URI in a hidden field in the form:

 <input name="redirect" type="hidden" value="<?= $this->uri->uri_string() ?>" /> 

And then redirect if validation fails:

 redirect($this->input->post('redirect')); 

Or you can set the redirect URL in the flashdata session variable:

 // in the method that displays the form $this->session->set_flashdata('redirect', $this->uri->uri_string()); 

And then redirect if validation fails:

 redirect($this->session->flashdata('redirect')); 
+5
source

I have expanded the url for this.

https://github.com/jonathanazulay/CodeIgniter-extensions/blob/master/MY_url_helper.php

In your controller:

 $this->load->helper('url'); redirect_back(); 

Just put MY_url_helper.php in application/helpers and you're good to go.

+11
source

Well, usually you need to do this (pseudo code for now):

  • if form_validation == false → the form is either not submitted yet or the verification is not completed → load the form view;
  • if form_validation == true → do the processing.

This means that you must remain in the same controller. Your code should already be doing this behavior, which is the intended one.

If you still feel like redirecting, call the appropriate function:

 redirect('updatebatch/get/40','refresh'); // assuming 'updatebatch' is the name of your controller, and 'sundial' just a folder 
0
source

I created a function inside the library to create redirects when I need it.

 <?php if (!defined('BASEPATH')) exit('No direct script access allowed'); class Functions { public function generateRedirectURL() { $CI =& get_instance(); $preURL = parse_url($_SERVER['REQUEST_URI']); $redirectUrl = array('redirectUrl' => 'http://' . $_SERVER['SERVER_NAME'] . $preURL['path']); $CI->session->set_userdata($redirectUrl); } } //End of the file 

and if you want to create a redirect to this page, just write in the function:

 $this->load->library('functions'); //you can put it in the autoloader config $this->functions->generateRedirectURL(); 

Then you only need to call:

 redirect($this->session->userdata['redirectUrl']); 
0
source

Source: https://habr.com/ru/post/900312/


All Articles