Symfony 5.1 $form->getData() Always Null (Basic Form)

This Symfony form question has been asked 100 times (and I’ve read ALL of the responses), but none are working for me. I have a class (Employer), a form (Preview.html.twig), and a controller (DefaultController.php). No matter what I try, I still get null values for the form fields. The form displays properly and I’m not saving to a database (I just want to dump the variables, then I’ll move on to db action). This has consumed weeks of my life and any assistance is sincerely appreciated.

The Default Controller (DefaultController.php)

    namespace AppController;
    use SymfonyBundleFrameworkBundleControllerAbstractController;
    use SymfonyComponentHttpFoundationResponse;
    use SymfonyComponentHttpFoundationRequest;
    use SymfonyComponentRoutingAnnotationRoute;
    use AppEntityEmployer;
    use AppFormEmployerType;

 class DefaultController extends AbstractController
 {  /**
    * @Route("/preview", name="preview")
    public function preview(Request $request)
          $employer = new Employer();
          $form = $this->createForm(EmployerType::class, $employer, ['csrf_protection' => false]);
          //the next two lines were added to force the form to submit, which it wasn't doing prior to
          if ($request->isMethod('POST')) {
            if ($form->isSubmitted() && $form->isValid()) {
                  $employer = $form->getData();
                  dump($form);     /****** ALL ENTRIES FROM THIS DUMP ARE NULL. *****/
                  exit;            /***Added to capture dump ******/
                  return $this->redirectToRoute('homepage');  /** Works when the exit is removed ***/

              return $this->render('preview.html.twig',
                                    ['form'=> $form->createView()]

The Employer Class (Employer.php)

namespace AppEntity;

class Employer
  protected $companyName;
  protected $companyAddress;

  public function setCompanyName($companyName)
  { $this->companyName = trim($companyName); }

  public function getCompanyName()
  { return $this->companyName; }

  public function setCompanyAddress($companyAddress)
  { $this->companyAddress = trim($companyAddress); }

  public function getCompanyAddress()
  { return $this->companyAddress; }

Form Builder (EmployerType.php)


namespace AppForm;

use AppEntityEmployer;
use SymfonyComponentFormAbstractType;
use SymfonyComponentFormFormBuilderInterface;
use SymfonyComponentOptionsResolverOptionsResolver;
use SymfonyComponentFormExtensionCoreTypeTextType;
use SymfonyComponentFormExtensionCoreTypeSubmitType;

class EmployerType extends AbstractType
    public function buildForm(FormBuilderInterface $builder, array $options)
            ->add('companyName', TextType::class, ['label' => 'Company Name', 'required' => false])
            ->add('companyAddress', TextType::class, ['label' => 'Address', 'required' => false])
            ->add('submit',SubmitType::class, ['label' => 'Submit & Preview'])
            ->getForm()   //I've added and removed this line multiple times. Not sure if its needed.

    public function configureOptions(OptionsResolver $resolver)
            'data_class' => Employer::class,

For Display (Preview.html.twig) ** Displays Form Correctly **

{{ form(form) }}

A few rabbit holes:

  1. The site is running on localhost (Symfony, Apache, MySQL).
  2. The form input is sent via POST.
  3. The Default Controller redirects after the Submit; the "exit" was added to pause my code.
  4. The form is not embedded. I’ve scaled back the entire project because I thought the embedded form was the issue.
  5. I changed the method to PUT and can see the form values appended to the URL, but $employer = $form->getData() still populates $employer with null values.
  6. I tried to get individual form fields upon submit using $form->get(‘companyName’)->getData(); The data remains null.

I’m out of ideas on how to save the form data to the Employer object.

Source: Symfony Questions

Was this helpful?

0 / 0

Leave a Reply 0

Your email address will not be published. Required fields are marked *