eZ Community » Blogs » Benjamin Choquet » Managing Forms in eZPublish 5

By

Benjamin Choquet

Managing Forms in eZPublish 5

Thursday 20 February 2014 2:27:40 pm

  • Currently 5 out of 5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

TL;DR: Heliopsis open sourced a bundle allowing you to easily leverage the power of Symfony forms component with your eZPublish contents.

Oldies are not always goldies

Forms have always been a pain to implement in eZPublish. Until version 4, you had two ways to create a form on your site: use information collection or a custom module.
The preferred way was to use eZ's information collection feature on a custom content class. You basically had to create a new class for each of your forms, add an attribute for each of your form fields and flag them as information collectors. You could useany of eZ's datatype for your fields and with a few template overrides you were ready to display a basic form on your site.Data would be stored in database upon form submission and you could even add a few settings in collect.ini to get an email to be sentas well or redirect to a confirmation page.
When you needed a bit more advanced features like custom data handling, advanced form validation or funnels, you could create a custom module. You then had to create almost everything from scratch, from your entire form html template to your custom handlinglogic not forgetting to validate input data.

Enter Symfony

You all know that since a few months eZPublish has been ported to a full stack Symfony, meaning we now have a whole new set ofhigh quality tools in our box. You may also know that Symfony comes with two great components: forms and validation. It is now easy to follow the book and create a symfony controller with its dedicated route, build a custom form and do whateveryou need with the data:

But what if you want to associate your form with eZPublish contents? You want to let your admin user define where each form shouldbe shown in content tree. Maybe you'd even like them to type the form introduction in a ezxmltext attribute. Well it can be easilyarranged by making your controller extend eZ's ViewController and declare it in your view provider's configuration:

But I don't want to write all this code!

Lazy as you are, you surely are a good developer, you little crybaby :) At Heliopsis we don't like to write the same thing multiple times eitherso we abstracted that away and released a bridge bundle including a controller and a handful of PHP interfaces that we hopeshould match most of the use cases. It is under the GPL v2 license so you can freely use and redistribute it with your eZPublish projects. It is available both on [packagist](https://packagist.org/packages/heliopsis/ezforms-bundle) and [github](http://github.com/heliopsis/ezforms-bundle) (By the way we are open to pull requests :)
Once you've added the bundle to your project you can basically concentrate on what's relevant to your project instead of the cementwe just saw. It's a 2 steps process:

1- Define your custom handlers

They're responsible for finding the right form for the right location and then to properlyhandle the form data (for the sake of brevity I'll omit the handling side of this but it is as much straightforward to use):

2- Configure eZ's view provider to use the bundle's controller and your own twig template

Et voilà !

There's more to this bundle that this basic example. You also can configure data handling, redirections and even use a facadeto define more complex logic. All those features are documented on project's repo on github
We hope the eZPublish community will grab this tool and use it to build great form related stuff (advanced data handlers,form builder field types, whatever comes to your mind) and are eager to read your feedback.

Proudly Developed with from