In my Symfony 4.4 application I have the following code inside my controller. I am attempting to pre-populate the form based on previous submissions or data pulled from the database. Importantly, the DetailsType
form includes multiple entities so it's not a clean 1 entity per form setup here.
$postVars = $request->request->all();
$formData = [];
if (count($postVars) > 0) {
$formData = $postVars['crmbundle_register_details'];
}
$form = $this->createForm(DetailsType::class, $formData, [
'attr' => ['class' => 'reCaptchaForm'],
]);
$form->setData([
'firstname' => $person->getFirstname(),
'lastname' => $person->getLastname(),
'email' => $person->getEmail(),
'country' => $person->getCountry(),
'timezone' => $person->getTimezone()
]);
My problem is if I try to pre-populate the form with setData
above it does not work.
If I do it individually as below it works, but I can't see why. I'd prefer to pass setData
an array rather than call setData
multiple times.
$form->get('firstname')->setData($user->getFirstname());
$form->get('lastname')->setData($user->getLastname());
$form->get('email')->setData($user->getEmail());
$form->get('country')->setData($user->getCountry());
$form->get('timezone')->setData($user->getTimezone());