A function in the controller User
loads the signup form, eg:
public function signup()
{
$this->load->view('form_view');
}
So the signup form url will be like root/user/signup
(ignore index.php)
The form_view
has a form, which is submitted to another method in the same controller, lets say process()
, eg.
<? echo form_open('user/process') ?>
Once the form is submitted, it goes to the user/process
, which contains the validation code. If there is any validation error, It would load the form_view
again and pass the error data to display on the form.
if ( $this -> form_validation -> run() === FALSE )
{
//$this -> load -> vars( $data );
$this -> load -> view( 'form_view' );
}
Everythi开发者_开发技巧ng works fine. But now since the same form_view
is loaded from the user/process
function, the url of the form will change from:
root/user/signup
to
root/user/process
Because the form was submitted to the url user/process
and the form_view
is being called from the process()
. Is it possible to somehow redirect or maintain the original form url(root/user/signup
) when the form_view
is loaded from the process function in case of errors?
Thanks.
Using the same method to for and validation you can try something like
if( count($this->input->post()) > 0 )
{ // do validation
if ($this->form_validation->run())
{
// initilize and
redirect('logged');
}
}
$this->load->view( 'form_view' );
you always get the same method, as you asked
I've done this a different way. I actually have one controller to do all this.
if ( $this -> form_validation -> run() === TRUE )
{
code
code
redirect('ANOTHER_URL_WITH_VALIDATION_CODE_DISPLAY');
}
$this -> load -> view( 'form_view' );
Sorry about that, I meant to say one controller NOT one method. If you're just looking for form validation, then throw it all into one controller. Have CI's built-in form_validation function do the validation for you. IF you require backend validation (i.e. username check), you can create a custom validation rule called a "callback" and have the form validate against that as well.
Ideally your controller will check all fields, if it passes, it should redirect to onward to the next page w/ any information you want to display (i.e. "Thank's for registering!"). If it fails the check, it should load the view page again; you'll need to repopulate the fields obviously.
See my response here for a detailed explanation of how/why the Codeigniter form validation should be called:
Question about how redirects should work
精彩评论