Callback Functions To Get/Set Form Fields
Description
Use callback functions to get/set form fields. Data mapper is no longer needed when getter/setter methods don’t directly correlate to property names.
Syntax
// src/Form/Type/ProductType.php
namespace App\Form\Type;
use App\Entity\Person;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Form\FormInterface;
class PersonType extends AbstractType
{
public function buildForm(FormBuilderInterface options)
{
person, FormInterface form): string {
return person->getUserData()->getFirstName();
},
‘setter’ => function (Person &name, FormInterface form): void {
person->rename($name);
},
])
// ...
;
}
// ...
}