Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
Multi-Step Forms
  • So i just started working on implementing a multi-step form on a project i'm working on. I've never done something similar before, all forms i've done in the past have been single page forms.

    Basically i want to throw some code out here, see what others might think of how i'm implementing it and get some input from others that might have done multi-step forms in the past that won't mind sharing their thoughts and ideas.

    Right as of now the code is quite bare, i've mostly been toying around with it a bit. 

    Traversing trough the two implemented steps are working fine, and i save the data submitted from the first step into a session variable so that the user can easily traverse back to step 1 (values still there) and change values to something else if they so wish.



    public function action_new()
    {
    // set our default form template to load
    $form = 'vehicles/add/form_step1';

    // get uri
    $uri = \Uri::segment(4);

    // if we have a post action
    if (\Input::post())
    {
    if (\Input::post('step1_submit'))
    {
    \Session::set('step1_values', array(
    'name' => \Input::post('name'),
    'email' => \Input::post('email'),
    'phone' => \Input::post('phone'),
    ));
    }
    }

    // switch our uri to decide course of action
    switch ($uri)
    {
    case 'step-1':
    $form = 'vehicles/add/form_step1';

    if (\Session::get('step1_values'))
    {
    $values = \Session::get('step1_values');
    }
    break;
    case 'step-2':
    $form = 'vehicles/add/form_step2';
    break;
    }

    $content = \Theme::instance('vehicle_add');
    $content->set_template('vehicles/add_new');

    if (isset($values))
    {
    $content->set_partial('form', $form)->set('values', $values);
    }
    else
    {
    $content->set_partial('form', $form);
    }

    $theme = \Theme::instance();
    $theme->set_partial('content', $content);
    }

    So that's it right now. In the final version i plan on implementing most if it with the help from fieldsets with validation and such. The user will be able to traverse trough all the steps at their own will as long as the previous step has been submitted and validated.

    Then upon final submission all the steps data will be pulled from their set sessions and inserted into their respective database tables.
  • I use different methods for the steps, so /controller/step1, /controller/step2, etc.  It will avoid the tangled mess of code in your method, due to different bits of logic you need to prep and validate a form.

    I also do the validation of the post directly, and only redirect to the next step if the form validated). It's much more user-friendly then validating at the end, and then go 3 steps back...

    I use the same session storage mechanism. In every step method, I check if I have a session key for the previous step, and if not I redirect back:

    \Session::get('step2', false)) or \Response::redirect('controller/step1);

    This means if someone requests step16 directly, it will bubble all the way back to step1.
  • That's good thinking. Don't know why i didn't think of it like that! Will definitely try to do it like that instead, code will be much cleaner.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

In this Discussion