9951 explained code solutions for 126 technologies


php-symfonyHow to use the PHP Symfony form?


The Symfony form component provides a set of classes to help you build complex, database-backed forms quickly and easily.

To use the Symfony form, you need to create a form class that extends the AbstractType class. This class will contain the form fields and their configuration.

<?php

namespace App\Form;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;

class MyFormType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('name', TextType::class)
            ->add('email', EmailType::class)
            ->add('save', SubmitType::class)
        ;
    }
}

The buildForm method is used to define the form fields and their configuration. In this example, we have added a name field of type TextType, an email field of type EmailType, and a save button of type SubmitType.

To render the form in a template, you can use the form_start, form_row, and form_end Twig functions:

{{ form_start(form) }}
    {{ form_row(form.name) }}
    {{ form_row(form.email) }}
    {{ form_row(form.save) }}
{{ form_end(form) }}

To process the form submission, you can use the handleRequest method of the form object:

$form->handleRequest($request);

if ($form->isSubmitted() && $form->isValid()) {
    // ...
}

Helpful links

Edit this code on GitHub