use Symfony\Component\Form\FormInterface; // Creating a form object $form = $this->createFormBuilder() ->add('name', TextType::class) ->add('email', EmailType::class) ->add('color', ChoiceType::class, [ 'choices' => [ 'Red' => 'red', 'Green' => 'green', 'Blue' => 'blue' ] ]) ->getForm(); // Handling form submission $form->handleRequest($request); if ($form->isSubmitted() && $form->isValid()) { // Retrieving form data $data = $form->getData(); // Printing the user's name, email, and favorite color echo 'Name: ' . $data['name'] . '
'; echo 'Email: ' . $data['email'] . '
'; echo 'Favorite Color: ' . $data['color'] . '
'; }
use Symfony\Component\Form\FormInterface; use Symfony\Component\Form\DataMapperInterface; // Creating a custom data mapper class MyDataMapper implements DataMapperInterface { public function mapDataToForms($data, $forms) { // Mapping nested form data $forms['address']->setData([ 'street' => $data['address']['street'], 'city' => $data['address']['city'], 'state' => $data['address']['state'] ]); } public function mapFormsToData($forms, &$data) { // Mapping nested form data $address = $forms['address']->getData(); $data['address'] = [ 'street' => $address['street'], 'city' => $address['city'], 'state' => $address['state'] ]; } } // Creating a form object with nested forms and the custom data mapper $form = $this->createFormBuilder() ->add('name', TextType::class) ->add('email', EmailType::class) ->add('address', FormType::class, [ 'data_class' => Address::class, 'data_mapper' => new MyDataMapper(), 'property_path' => 'address' ]) ->getForm(); // Handling form submission $form->handleRequest($request); if ($form->isSubmitted() && $form->isValid()) { // Retrieving form data $data = $form->getData(); // Printing the user's name, email, and address information echo 'Name: ' . $data['name'] . 'In both examples, the `getData()` method is used to retrieve the form data. The Symfony Form component library is used to create and manage the forms.
'; echo 'Email: ' . $data['email'] . '
'; echo 'Address: ' . $data['address']->getStreet() . ', '; echo $data['address']->getCity() . ', '; echo $data['address']->getState() . '
'; }