ExpressionEngine CMS
Open, Free, Amazing

Thread

This is an archived forum and the content is probably no longer relevant, but is provided here for posterity.

The active forums are here.

Form validation redirect to same page

December 01, 2009 11:46pm

Subscribe [3]
  • #1 / Dec 01, 2009 11:46pm

    sqwk

    83 posts

    I have a form that is assembled into a variable within the controller, and then passed to the viewfile.

    $searchform .= '<input type="text" name="room_number" value="'.set_value('room_number').'" id="room_number" maxlength="2">';
    
    etc for more elements…

    This form submits to the same controller, which checks for $this->input->post and displays the relevant results somewhere further down the page. The form fields are populated again with the previous data with functions like set_value().

    So far so good. (It works)

    Now I would like to validate some of the input fields…

    $this->load->library('form_validation');        
    $this->form_validation->set_error_delimiters('<div class="error">', '</div>');
    $this->form_validation->set_message('is_natural', 'This is not a number.');
    $this->form_validation->set_rules('room_number', 'Anzahl Räume', 'is_natural');

    and display the relevant error messages:

    $searchform .= form_error('room_number');

    Unfortunately the whole thing falls apart: Form errors are not shown and the form fields are no longer populated with the old entries, not on submitting and not on error (not a natural number) How do I get both to work?

  • #2 / Dec 02, 2009 2:17am

    Cro_Crx

    247 posts

    Have you set the form validation fields ? And run the validation according to the documentation here ==>

    <a href="http://ellislab.com/codeigniter/user-guide/libraries/form_validation.html">http://ellislab.com/codeigniter/user-guide/libraries/form_validation.html</a>

    ??

  • #3 / Dec 02, 2009 8:34am

    sqwk

    83 posts

    Yes. The only bit that is missing is the if statement as it is not needed. (I am redirecting to same page if the form is filled out correctly.)

    if ($this->form_validation->run() == FALSE) {
        $this->load->view('myform');
    } else {
        $this->load->view('formsuccess');
    }
  • #4 / Dec 02, 2009 8:44am

    Cro_Crx

    247 posts

    but your still running

    $this->form_validation->run();

    at some point, yeah ?

    If you are and still have problems. Post a snippet of your code within your controller that’s having the problems.

  • #5 / Dec 02, 2009 8:57am

    sqwk

    83 posts

    $this->form_validation->run();

    is running. I did a bit of rearranging and used the if statement instead: it looks like the form is being validated, but the errors don’t show up and the fields are not re-populated.

    Right now I am adding the errors to the same variable as the form, like so:

    $searchform .= '<label for="room_number">Anzahl Räume</label>';
    $searchform .= '<input type="text" name="room_number" value="'.set_value('room_number').'" id="room_number" maxlength="2">';
    $searchform .= form_error('room_number');

    where $searchform is passed to the viewfile.

  • #6 / Dec 02, 2009 8:59am

    oliur

    40 posts

    I am on the same boat, would love to see a solution. thanks.

  • #7 / Dec 02, 2009 9:10am

    Cro_Crx

    247 posts

    Is there any reason why your using a PHP variable to store the form information ?
    Your application would be more robust and probably easier to manage if you passed the information to a view. You can get the benefits of an output buffer as well.

    If you put your search form into a view and then just pass it the data needed it might get around your problem. Other than that, i can’t think of anything to try.

  • #8 / Dec 02, 2009 7:35pm

    sqwk

    83 posts

    Thanks, cro_crx, that solved the problem.

    Standing in front of a slightly different problem now though:

    How do submit form data to a different controller if it validates, but to the same controller if it doesn’t?

  • #9 / Dec 02, 2009 9:22pm

    Cro_Crx

    247 posts

    You can’t really, although there’s a redirect() function in the uri helper so you can run that once the validation is true.

  • #10 / Dec 03, 2009 4:27am

    DinneBolt

    1 posts

    Cro_Crx is right. You should use redirect() function. I use it for my login form.

    if ($admin_type == 'admin_a') {
        redirect('controller_a');
    }
    else if ($admin_type == 'admin_b') {
        redirect('controller_b');
        }
    //you can even redirect to a function
    else if ($admin_type == 'admin_c') {
        redirect('controller_c/function_c');
    }
    //or just want to load a view file
    else {
        $this->load->view('login');
    }

    I hope it can help.

  • #11 / Dec 03, 2009 10:13am

    sqwk

    83 posts

    Can you pass the formdata with the redirect function?

  • #12 / Dec 03, 2009 10:38am

    Cro_Crx

    247 posts

    Can you pass the formdata with the redirect function?

    No, you can’t. If you have to do that then you should probably think about your application design some more. There shouldn’t’ be any situations where you would need to process data across multiple controllers.

    If you give us a general idea of what you want to do i’m sure someone can point you in the right direction.

    All you probably need to do is process the form data and then redirect afterwards, this would solve the problem of redirecting with data.

  • #13 / Dec 03, 2009 10:48am

    sqwk

    83 posts

    I am searching a database. There is a basic search form on the homepage (1st controller). It the data validates, the user should be directed to results page (2nd controller), if it doesn’t the errors are shown on the homepage (1st controller).

    On the results page there is a more detailed search form, which submits to itself. It the form data doesn’t validate the result set from the last search is shown.

    At the moment I am tackling this by getting the form fields, encoding them into a url segment, redirecting to the second controller, where they are decoded and the results are displayed. The search form on this second page simply redirects to itself. Depending on whether that data validates, the page loads again with errors or the user is redirected to a new url (result set).

    It works fine, yet it seems incredibly messy. It does have the advantage that searches are bookmarkable, but that is about it…

.(JavaScript must be enabled to view this email address)

ExpressionEngine News!

#eecms, #events, #releases