What is a form context in Symfony2

I am starting out with Symfony2 and I am trying to understand the form component. I look at this page http://docs.symfony-reloaded.org/guides/forms/overview.html

And I can understand how we create class classes, but what is confusing is how we actually use these forms in our controller.

$form = ContactForm::create($this->get('form.context')); 

Does anyone have a more detailed explanation of the form.context part of this code and the actual process of using forms inside controllers?

Thanks.

+4
source share
1 answer
Service

form.context by default a Symfony\Component\Form\FormContext . Here is the full definition of this service:

  <service id="form.context" class="%form.context.class%"> <argument type="collection"> <argument key="validator" type="service" id="validator" /> <argument key="validation_groups">%form.validation_groups%</argument> <argument key="field_factory" type="service" id="form.field_factory" /> <argument key="csrf_protection">%form.csrf_protection.enabled%</argument> <argument key="csrf_field_name">%form.csrf_protection.field_name%</argument> <argument key="csrf_provider" type="service" id="form.csrf_provider" /> </argument> </service> 

In fact, this is a very simple object that simply prepares some basic options used by almost every form, i.e. validator, CSRF protection and factory field.

In fact, the code you posted is equivalent to:

 $form = new \Symfony\Components\Form\Form(null, array( 'validator' => $this->get('validator'), 'validation_groups' => ... ... )); 
+6
source

Source: https://habr.com/ru/post/1339046/


All Articles