2

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' );   

}

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

1
  • 1
    I'd use the method in the CI docs, which will keep the original URL, but not post to another method in your controller codeigniter.com/user_guide/libraries/… Commented Jul 18, 2011 at 12:08

3 Answers 3

1

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

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks. Just wondering why count($this->input->post()) > 0, would this also be fine? if( $this->input->post() ) ?
Yes, you are right. The function returns FALSE (boolean) if there are no items in the POST. You can also check for the existence of one of the fields, let's say $this->input->post('username') with the same results.
1

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.

3 Comments

I think that's merely a comment.
@luckytaxi can you give some more info please? Did you mean i should use only one controller function to load the default form and the same function to load the function with validation erros? Could you give some example of how you did? thanks.
@Roman, See Nick's comment. Its exactly what you are looking for.
0

See my response here for a detailed explanation of how/why the Codeigniter form validation should be called:

Question about how redirects should work

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.