Symfony Forms
Symfony Forms
Definiciones
Form Type
In other projects, it’s common to differentiate between “forms” and “form fields”. In Symfony,
all of them are “form types”:
a single <input type="text"> form field is a “form type” (e.g. TextType );
a group of several HTML fields used to input a postal address is a “form type” (e.g.
PostalAddressType );
an entire <form> with multiple fields to edit a user profile is a “form type” (e.g.
UserProfileType ).
https://symfony.com/doc/current/forms.html#form-types
FormBuilder
“form builder” object allows you to describe the form fields using a fluent interface.
FormView
FormFactory
A form factory retrieves the type hierarchy from the loaded extensions and uses them to
configure new FormBuilder and FormView objects.
Las Forms son objetos construidos por la form factory.
Se puede crear una factory fácilmente $formFactory = Forms::createFormFactory();
Esa factory es básica puede crear forms básicas, pero le falta soporte para:
Request Handling. (se puede usar el componente Httpfoundation , ejemplo
$formFactory = Forms::createFormFactoryBuilder()-
>addExtension(new HttpFoundationExtension())-
>getFormFactory(); )
CSRF protection. (Protection against CSRF attacks is built into the Form component, but
you need to explicitly enable it or replace it with a custom solution.)
Templating. (se puede usar el componente Twig )
Translation. (se puede usar componente Translation )
Validation (Se puede usar el componente Validation .
Architecture
https://webmozart.io/blog/2012/03/06/symfony2-form-architecture/
Abstraction: providing suitable data structures for describing and reusing your code. (conversiones
back and forth)
Extensibility:
Specialization: a simple example is to extend [the above] date selector to also show selectors for
the time.
Mixins: Mixins, on the other hand, allow to attach functionality to existing objects without the
need to specialize them. Example: change all existing fields to include an asterisk (“*”) in their
label if they require user input.
<?php
class Base {
public function sayHello() {
echo 'Hello ';
}
}
trait SayWorld {
public function sayHello() {
parent::sayHello();
echo 'World!';
}
}
The Core extension provides all field definitions (called form types que según lo de arriba son las forms
y form fields) implemented by the framework.
The Validation extension integrates the Symfony2 Validator to implement form validation.
The DI extension adds support for Symfony2’s Dependency Injection component.
The CSRF extension adds CSRF protection to forms.
The Doctrine 2 extension (shipped with the Doctrine bridge) adds a Doctrine-specific drop down field
and provides components that let forms know about Doctrine metadata
A form and all of its children can be represented by the same data structure that implements the Composite
pattern (como un árbol).
The main implementation of FormInterface is the class Form , which uses three components to
do its work:
A data mapper distributes the data of a form to its children and merges the data of the children
back into the form’s data. (para abajo y para arriba)
Two chains of data transformers convert values between different representations.
An event dispatcher allows you to execute custom code at predefined points during form
processing.
As can be seen in the diagram, a form has three different representations throughout its
lifecycle:
During construction, it is represented by a hierarchy of FormBuilder objects.
In the controller, it is represented by a hierarchy of Form objects.
In the view, it is represented by a hierarchy of FormView objects.
Because the configuration of form builders and form views is repetitive, Symfony2 implements form
types that group such configuration.