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 a symfony feedback form

We'll start with the form definition itself. We'll create a model class for the form that will define the what information we want our users to give, that is:

  • User Full name
  • User email
  • Message

Let's create the Model class:

 

Next, we'll create a dedicated Symfony form type. Form types define how forms will be created: what fields they should contain, what's the type of each field (like text, email, choice list or even a custom type containing subfields), what kind of data they represent, what data should be transmitted to the view, etc...

Our type will be quite simple, defining our fields and stating that data should be mapped into our model class:

  • Note the getName() method: it must return the unique identifier of your form type.
  • The setDefaultOptions() is where we tell Symfony to map form data to our model class
  • Our form elements are defined in our buildForm() method with FormBuilderInterface::add() method:
     
     
    • First parameter is the field name. If your model has a property with the same name, it will be bound to the field.
    • Second parameter is the field type. Symfony provides a bunch of them but you can create your own. In fact, that's exactly what we're doing right now, that's what the getName() method is for :)
    • Third parameter is a list of options. Options depend on the field type but the label is common to all types.
  • We created the submit button as a special field with the submit type

Finally, we'll declare our new type in the DIC so that Symfony will know what class our type name relates to. Add your service definition to your services.yml file:

 

The important part is the tag section as it tells Symfony the service is a form type called acme_feedback (note that is must be the same as the value your getName() method returns).

 

Printable

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

Author(s)

Proudly Developed with from