6

I am using Codeigniter's validation class to validate my form. Could you please tell me how to redirect to the previous page from controller if found any validation error?

In my controller:

if ($this->form_validation->run() == FALSE){

  //**** Here is where I need to redirect  

} else  {
     // code to send data to model...           

  }                             
black_belt
  • 6,601
  • 36
  • 121
  • 185

4 Answers4

11

I extended the URL helper 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 the MY_url_helper.php in application/helpers and you're good to go.

Jonathan
  • 2,968
  • 3
  • 24
  • 36
  • A little feedback: this URL helper worked perfect for me. Thank you very much. In my opinion it is best to make helpers for things like this. – Alex Spencer Jun 01 '13 at 07:24
  • @AlexSpencer Thanks! Just wanna put a note here that http is hardcoded on line 17 (not a big problem but still). That should be fixed, It's open for pull requests. – Jonathan Jun 11 '13 at 12:23
5

UPDATE

You want to post a form, validate it, then show the form again with the validation errors if validation fails, or show something entirely different if validation passes.

The best way to do this is to post a form back to itself. So the action of your form would be action="". This way, in your method, you can check to see if the form was 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 the validation fails:

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

Or you can set the redirect url in a flashdata session variable:

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

And then redirect if the validation fails:

redirect($this->session->flashdata('redirect'));
swatkins
  • 13,530
  • 4
  • 46
  • 78
  • Thanks for your reply. The first two of your codes that you posted are working perfectly accept for displaying the validation errors. So I was trying to use the second two of your codes that you posted but since I have very little knowledge on PHP/codeigniter, I am not sure where to use those. Should I paste this "$this->session->set_flashdata('redirect', $this->uri->uri_string());" on the controller I posted above or in the form I mentioned about? Thanks again :) Sorry for my stupid question. :( – black_belt Oct 28 '11 at 19:47
  • Either way, you won't get the validation errors unless you pass those as well. I'll update my answer to reflect that you need validation errors displayed. – swatkins Oct 28 '11 at 19:53
0

Well, usually you should do like this (pseudocode for now):

  • if form_validation == false --> the form is either not submitted yet or validation failed --> load the form view;
  • if form_validation == true --> do the processing.

This means you have to stay within the same controller. Your code should already be doing this behaviour, which is the intended one.

If you still feel the urge to redirect, call the appropriate function:

redirect('updatebatch/get/40','refresh');
// assuming 'updatebatch' is the name of your controller, and 'sundial' just a folder
Damien Pirsy
  • 25,319
  • 8
  • 70
  • 77
  • Thanks for your reply. Sorry,I should have stated this earlier that- in http://localhost/sundial/updatebatch/get/40- the portion of "40" is variable. It may change time to time. in that case what should I do? Just to make it clear, here the 40 is the id number of a particular row of a table. I am just using the id to fetch data. Thanks a lot for your help :) – black_belt Oct 28 '11 at 19:19
0

I have created a function inside a library to create redirects when I need them.

<?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 when you want to create the redirect to that page, just write on 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']);
jonaypelluz
  • 368
  • 4
  • 13