Forms - MiguelFieira/AMO-HANDBOEK GitHub Wiki
This guide will explain forms in Symfony. Forms are useful tools for the user to interact with your site.
The FormBuilder
The FormBuilder is used for creating form templates in Symfony, the FormBuilder can be accessed in the Controller and the FormType files.
// Inside the Controller
$builder = $this->createFormBuilder();
// Don't forget this import
use Symfony\Component\Form\FormBuilderInterface;
// Inside the FormType
class MyAwesomeType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
}
}
Where to make your form
If you want a form for an Entity or if you want it to be reusable for any other reason it is a best practice to make your form inside the FormType.
If you dynamically want to render a form and reusability among multiplw controllers isn't much of a concern, you could make your form inside your Controller.
You can call the add() method on a Form in both the FormType and Controller. So you can create a template in a FormType and extra fields in the Controller. Useful in cases in which making a new FormType simply isn't worth it. With this knowledge you will be unstoppable.
If you create a CRUD via make:crud, a FormType is automatically created for your Entity.
Creating your form template
You can fill your form with the add() method. The two most important things to add are the first two arguments: the field name and the field type. There is a third argument you can give, which should be an array, this argument can contain all sorts of data, such as whether it's required or not and even HTML attributes you want to add.
$builder
->add('title', TextType::class, ['attr' => ['id' => 'title', 'placeholder' => 'title'], 'required' => false])
->add('submit', SubmitType::class);
You can find a full list of available form field types here.
Tip: setting a default Entity
If you are 100% sure that a form will only be used for one type of Entity, you could add a configureOptions() method to your FormType class. Inside of this method you can specify your default Entity by setting it with the setDefaults() method in the OptionsResolver.
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults([
'data_class' => YourAwesomeClass::class,
]);
}
The Form
In the Controller the FormBuilder can return a form object if you call the getForm() method on the FormBuilder. You can store this form in a variable for easier access.
// Here we create a form and immediately call getForm() and assign it to the varable $form.
$form = $this->createFormBuilder($task)
->add('task', TextType::class)
->add('dueDate', DateType::class)
->add('save', SubmitType::class, ['label' => 'Create Task'])
->getForm();
// You could also create a form beforehand and call the getForm() method later on.
$notAForm = $this->createFormBuilder($task)
->add('task', TextType::class)
->add('dueDate', DateType::class)
->add('save', SubmitType::class, ['label' => 'Create Task'])
$aForm = $notAForm->getForm();
If you already have a form template in your FormType file you can access that form in this manner:
$animalForm = $this->createForm(AnimalType::class);
// You can set default values by passing an Entity object to the createForm() function
$cutePuppy = new Animal();
$cutePuppy->setName('Iggy');
$puppyForm = $this->createForm(AnimalType::class, $cutePuppy);
Usually a form gets rendered on a webpage, you van do this by passing a FormView to a twig. To get the view from a Form, you need to call the createView() method.
// Here we pass a Form view to the twig. of course you can call the createView() function before passing it to the twig and store it in a variable for later use.
return $this->render('song/index.html.twig', [
'form' => $form->createView(),
]);
Processing forms
Something you would probably like to do is process the form data once the user has sent the form data back to the server.
You need to specify that the form needs to handle the incoming Request object. This is done with the handleRequest() method.
// Now the form knows that it has to handle the request
$form->handleRequest($request);
Once the form is able to be handled (very gently, forms are very sensitive :3), you can put a check in your function that checks if a form is submitted. You can do this very easily with a simple if-statement.
Read Processing data for more about processing data.
// This statement checks if the form is sumbitted and if the data is valid (it runs a validator, which by the way can be customised to for your needs)
if ($form->isSubmitted() && $form->isValid()) {
// ... do all the fun data processing you want to do here
}
If you have set your Entity in your FormType, you can just flush to get the data to the database.
if ($form->isSubmitted() && $form->isValid()) {
// Flushed Away
$this->getDoctrine()->getManager()->flush();
// You can call getData() to just get your entity without flushing
$form->getData();
}
If you haven't, you can get the field values from the getData()
if ($form->isSubmitted() && $form->isValid()) {
$form->getData()['your_field_name']
}