CodeIgniter Forums
how can redirect with validate - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: how can redirect with validate (/showthread.php?tid=52145)



how can redirect with validate - El Forum - 05-30-2012

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


how can redirect with validate - El Forum - 05-30-2012

[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-guide/helpers/url_helper.html



how can redirect with validate - El Forum - 05-30-2012

[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-guide/helpers/url_helper.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



how can redirect with validate - El Forum - 05-30-2012

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


how can redirect with validate - El Forum - 05-31-2012

[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



how can redirect with validate - El Forum - 05-31-2012

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



how can redirect with validate - El Forum - 05-31-2012

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


how can redirect with validate - El Forum - 05-31-2012

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


how can redirect with validate - El Forum - 05-31-2012

[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