eZ Community » Learn » eZ Publish » Create a feedback form in eZ Publish...

Create a feedback form in eZ Publish 5 using ezforms-bundle

Monday 12 May 2014 3:33:49 pm

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

Create location views twig templates

Now that we're all set up, let the actual coding begin. We're going to create two eZ Publish location views to use with our form content type:

  • the full view, will display our introduction message and the form itself when we navigate to the corresponding page on our frontend.
  • the confirm view will be used after form validation to display our confirmation message.

eZFormsBundle provides a controller specifically designed to handle forms. We'll use it instead of eZ Publish's default ViewController. Here's what happens under the hood:

  1. Controller finds which form should be displayed for current location
  2. It builds the form view and passes it to the view template in a form parameter.
  3. User fills in the form and submits data
  4. Controller tries to validate submitted data. If validation fails, we go back to step 2.
  5. If data validation succeeds, controller identifies what service should handle submitted data and passes it.
  6. Finally, if all goes well, controller redirects to the confirm view of current location

NB: as all data handling is done before redirecting, there is no need to use the dedicated controller to render theconfirm view. We can use eZPublish's ViewController.

The views templates are straightforward:

 

 

As you can see, Symfony makes it extremely simple to render a form from a twig template. All you need is calling the form() twig function passing it the form parameter. Of course you can customize form rendering but for the sake of clarity we'll stick to the default form theme.

Let's now tell eZ Publish when to use our controller and views. To do so, we must prepend some configuration of our own to ezpublish view matching logic. We'll edit our bundle's extension class to make it implement a special interface:

The load() method did not change but the PrependExtensionInterface introduced a prepend() method which we use to load configuration from a new ezpublish.yml file:

Both our location views are going to be used when ContentType identifier is form. In case of the full view, eZFormsBundle's controller will be specifically called.

Now that we told eZPublish that eZFormsBundle will be in charge of displaying our form contents, we need to tell it what kind of form it should display according to the location the user wants to view.

 

 

Printable

Printer Friendly version of the full article on one page with plain styles

Author(s)

Proudly Developed with from