Instructions for FormType in PHP Symfony (Part 1)

Tram Ho

Hello everyone today I will guide you, especially those new to the Symfony framework how to create FormType using common or common field types. I forgot to follow this post I will continue the next article on how to self-customize custom errors or how to check validations for special cases, don’t say much more, let’s start

Component form

Form components are basically a tool or something that will help you solve problems that often arise from end-users when users manipulate data such as adding, editing, and deleting. To create the Form you do the following:

To handle the data submitted we use the handleRequest () function, we only need to pass the parameter is the Request object.

Example: Create a simple form

Display the form on the view page:

Then when you come to this step, you will have for yourself a displayed form, but there are still problems as to validate (I will talk in the next article) or how to adjust the FieldType inside it. In my opinion, please continue reading below.

Text Fields

Text Fields in this group include field types that allow us to enter data such as TextType, TextareaType, NumberType, PasswordType, etc. The common options available in TextType are:

  • attr : This option allows us to add attributes to the field when displayed in HTML, the value entered is of the array type.
  • data This allows us to set values ​​for the field, the value of the input is all data types.
  • disabled : If you do not want the user to change data on the field then you use this option, the input value is boolean and the setting will be false.
  • label : set the label for this field
  • mapped : this option is when you want the field to be mapped with the same data structure as previously defined, for example, when you have defined an Enity / Model with so many attributes / attributes, when setting the value to true then It will automatically map to the Enity / Model that you have defined, and in case of false , you can create a field whose name does not have in the attributes / attributes of an Enity / Model. Temporarily is like that later I will have an article that talks more about this and how to use it in real life.
  • required : The name says it all

And there are many other options, but these are the most common options in the Text Fields group  Example: How to create a Text Field

Choice Fields

Choice Fields allows users to select one or more gen when it will be select tags, radio …. Within this group is ChoiceType, EntityType is frequently used. Since this is used frequently, this article I will only say these two

  1. ChoiceType : When this gene generates HTML, it is a select tag, we can change it to become a dropdown or select, a group of radio buttons
  2. EntityType : This one I often use it to create dropdown fish (when the gene is also select). The difference between these two is that: ChoiceType is only used to display simple data of hard set data such as using it to display values ​​in enums, for example, and EntityType is used for displaying Display data loaded from database. The options in the Choice Fields group are not much different from the Text Fields except:
  • choice_attr : Often used to add attributes to each select option’s tags
  • group_by : if you already know that 1 select it allows splitting up groups then this will work like that
  • multiple : this option allows us to select multiple (multi select / choice) or not.

Example: How to create a ChoiceType

Example: How to create an EntityType

That’s all in general, this should be enough

Date and Time Fields

The fields in this group will have a way of displaying on HTML as inputs that allow to set the day / month / year or time, it can display in many different ways via widgets. A quick glance at some of the options is very similar to Choice Fields and TextFields. Example: How to create a DateType:

Other fields such as: Field Group, Hidden Fields, Button are nothing special, but there are still a lot of things to say like validation, form collection …. In summary, I have introduced fields that are often Meet most, the next article I will guide on how to validate, custom validation on a form.



Share the news now

Source : Viblo