CodeIgniter Forums

Full Version: how can redirect with validate
You're currently viewing a stripped down version of our content. View the full version with proper formatting.

El Forum

[eluser]Kency[/eluser]
Hi !

I have problem when i try validate my form with CI form validate, but i want to redirect when i validate. it mean my url like

Code:
http://example.com/form

and when form is missing it call function checkform() and url will be

Code:
http://example.com/checkform

but i want my url is the same in first url

Code:
http://example.com/form

how can i do it with CI?

El Forum

[eluser]Abel A.[/eluser]
I'm not sure what you're asking here...

but, you can try reading this(scroll to the bottom): http://ellislab.com/codeigniter/user-gui...elper.html

El Forum

[eluser]Kency[/eluser]
[quote author="berkguy" date="1338446981"]I'm not sure what you're asking here...

but, you can try reading this(scroll to the bottom): http://ellislab.com/codeigniter/user-gui...elper.html
[/quote]


i mean when i submit form, my form call function checkform() from controller.

and url will be
Code:
http://example.com/checkform

but i dont want my url like that i need my url the same url with my form

Code:
http://example.com/form

El Forum

[eluser]Abel A.[/eluser]
You mean the callback when you use the CI form validation?

El Forum

[eluser]Kency[/eluser]
[quote author="berkguy" date="1338447337"]You mean the callback when you use the CI form validation?[/quote]

ok i post sample code to make sense what i mean

Code:
url: http://example.com/form

<?php

form_open("controller/checkform);
//content
form_close();

?>

and my controller

Code:
function checkform(){
$this->load->library("form_validation");
            //some rule for validate
            if($this->form_validation->run()==FALSE){
                $this->form()
}

url change from http://example.com/form to http://example.com/checkform

but i dont want my url change like that, if i use redirect(/form) my form doesn't show any error

El Forum

[eluser]Abel A.[/eluser]
If you're trying to call another controller, it means your code is not very organized. CI is pretty well structured. Try following this:

Code:
class Form extends CI_Controller {

public function __construct()
{
  parent::__construct();
  
}

public function index()
{
  
  $this->load->helper('form');
  $this->load->library('form_validation');
  
  $this->form_validation->set_rules('username', 'username', 'required|callback__checkform');
  $this->form_validation->set_rules('password', 'password', 'required');
  
  if ($this->form_validation->run() == TRUE)
  {
   //form is valid
  }
  
  //load view here
}

public function _checkform($password)
{
  if ()//username and password are good
  {
   return true;
  }
  else
  {
   return false;
  }
}
}

El Forum

[eluser]Abel A.[/eluser]
Also, don't use your controllers as functions for your application. Controller are meant to control your app and call views. If you need to create functions you can use models. Creating functions in your controller can lead to a security risk too. Notice that I added the _ to the checkform function so it's not accessible via url.

El Forum

[eluser]Kency[/eluser]
thank for your quick reply @berkguy

but my form just check some information about user who send his feedback to us.

and in my controller has many function it check check some form like that, i wondering how can i check it within index function?

El Forum

[eluser]Abel A.[/eluser]
That's what I'm talking about, if you're going to re-use a function in your controller, you should move the function to a model. Controllers should only include code that will only be used once. Save yourself the headache and just move the function to a model. It's a little extra work, but it's work it Smile