PHP code example of asmitta-01 / formflow-bundle

1. Go to this page and download the library: Download asmitta-01/formflow-bundle library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

asmitta-01 / formflow-bundle example snippets


// File: src/Form/UserType.php


// Imports...

class UserType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options): void
    {
        $builder
            ->add('email')
            ->add('first_name', TextType::class, [
                'attr' => [
                    'placeholder' => 'Enter your first name'
                ],
            ])
            ->add('last_name')
            ->add('phone_number')
            ->add('professional_qualification')
            ->add('gender', EnumType::class, [
                'class' => Gender::class,
                'mapped' => false,
                'expanded' => true,
                'label' => 'gender',
            ])
        ;
    }

    public function configureOptions(OptionsResolver $resolver): void
    {
        $resolver->setDefaults([
            'data_class' => User::class,
        ]);
    }
}


// src/Form/UserFlow.php


namespace App\Form;

use Asmitta\FormFlowBundle\Form\FormFlow;

class UserFlow extends FormFlow
{

    protected function loadStepsConfig(): array
    {
        return [
            [
                'label' => 'Name',
                'form_type' => UserType::class,
            ],
            [
                'label' => 'Contact',
                'form_type' => UserType::class,
            ],
            [
                'label' => 'Profession',
                'form_type' => UserType::class,
            ],
        ];
    }
}

// UserType class
...
    public function buildForm(FormBuilderInterface $builder, array $options): void
    {
        switch ($options['flow_step']) {
            case 1:
                $builder
                    ->add('first_name', TextType::class, [
                        'attr' => [
                            'placeholder' => 'Enter your first name'
                        ],
                    ])
                    ->add('last_name');
                break;
            case 2:
                $builder
                    ->add('email')
                    ->add('phone_number');
                break;
            case 3:
                $builder
                    ->add('professional_qualification')
                    ->add('gender', EnumType::class, [
                        'class' => Gender::class,
                        'mapped' => false,
                        'expanded' => true,
                        'label' => 'gender',
                    ]);
                break;
        }
    }


// in src/Controller/SomeController.php
final class SomeController extends AbstractController
{
    private $flow;

    public function __construct(UserFlow $flow)
    {
        $this->flow = $flow;
    }
 
    public function someMethod(): Response
    {
        $user = new User();

        // Get existing flow data from session if it exists
        $this->flow->bind($user);
        $form = $this->flow->createForm();

        if ($this->flow->isValid($form)) {
            $this->flow->saveCurrentStepData($form); // Save data in session
            $user = $form->getData();

            if ($this->flow->nextStep()) {
                $form = $this->flow->createForm(); // Go to the next step
            } else {
                // Persist data here
                $this->flow->reset(); // remove all data from the session
                return $this->redirectToRoute('some_route'); // redirect when done
            }
        }

        return $this->render(
            'custom_template.html.twig',
            [
                'form' => $form->createView(),
                'flow' => $this->flow,
            ],
        );
    }
}

// In our controller
...
        if ($this->flow->isValid($form)) {
            $this->flow->saveCurrentStepData($form);

            if ($this->flow->nextStep()) {
                $form = $this->flow->createForm();
            } else {
                $stepNumber = 1; // It is the step where 'first_name' field is displayed
                $user->setFirstName($this->flow->getStepData($stepNumber)['first_name']);

                // Persist data here
                $this->flow->reset(); 
                return $this->redirectToRoute('some_route'); // redirect when done
            }
        }
...

// Your Controller method
        return $this->render(
            'custom_template.html.twig',
            [
                'form' => $form->createView(),
                'flow' => $this->flow,
            ],
            new Response(status: Response::HTTP_UNPROCESSABLE_ENTITY) 
            // Without this Symfony Turbo will not load the new steps, it will stuck on the first step.
        );