Skip to content

Instantly share code, notes, and snippets.

@blixit
Last active May 20, 2017 19:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save blixit/2400dddec6313f4d335fb065d43b2be1 to your computer and use it in GitHub Desktop.
Save blixit/2400dddec6313f4d335fb065d43b2be1 to your computer and use it in GitHub Desktop.
Msf Bundle : An easy way to implement Multi Step Forms with symfony

MSF Bundle (Symfony 3)

During a simple project, I found that there were not any bundle providing the Multiple Step Form feature. Actually, I found one, https://github.com/craue/CraueFormFlowBundle . But for me, this bundle focuses too much on the Forms themselves, which was not relevant to me. For instance, I don't use to handle Form Event. Then, I came to the idea I could create a bundle which let developper interact with each form separately and then use a little well-configured system to manage the set of forms, the transitions between them, hydratation, ...

Therefore I created the MSFBundle. It's a little one with currently 5 or 6 classes and works out as a service. Its usage is very similar to form themselves to reduce learning time.

Bonus : with this system, it's easy to manage transitions even between MSF. You just have to configure actions at each step.

Example of action

  //loading MSFDataLoader entity
  $msfdataloader = $this->getDoctrine()->getRepository('AppBundle:MSF\MSFDataLoader')
      ->findOneBy(['id'=>1]);

  //using MSF service
  $msf = $this->container->get('msf.registration');
  // setting the specific MSFDataLoader to use
  $msf->setMsfDataLoader($msfdataloader); 
  // OR if the loader doesn't exist yet
  // $msf->init(new MSFDataLoader(), 'username'); 

  //similar to the native usage of symfony 3 forms
  $msf_form = $msf->getForm(); //FormInterface
  $msf_form->handleRequest($request);

  if($msf_form->isSubmitted() && $msf_form->isValid() ){
      //for instance, persist entity
      //you can also use configuration to set a callback to use for the form validation
      
      //go to the following form or be redirected to configurated location
      $msf->done();
  }
  
  return $this->render('MSF/default.html.twig', [
      'form'      => $msf_form->createView()
  ]);

Dependencies

  • Symfony 3
  • JMS Serializer (Need to install JMS)
  • Doctrine Orm Entity Manager
  • Router
  • Request Stack
  • Form Factory

Upcoming Features

  • Buttons configuration (previous, next, cancel)

Performances

I have not studied performance yet. My main doubt is about use of Serializer. To make easy the use of my service, I let developpers define their own validation methods. To do that, they need to access the whole MSF Data which is stored as a JSON string and to deserialize it.

Others usages

  1. You can attach one of your entities to a MSFDataLoader For instance, let create a MSF form to manage user registration. Let say the msf contains user, contact and role forms. You can add a MSFDataLoader field to your user entity. To dynamically attach the created user to this msfdataloader, go to the validation method of the 'user' configuration ( see method configure() ), set the field on the user object and then persist the user.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment