In my application, I have a form generated using a class like so:
public function buildForm(FormBuilderInterface $builder, array $options): void
{
$builder
->add('id', HiddenType::class, [])
->add('band', EntityType::class, [
'class' => Band::class,
'choice_label' => 'band',
'choice_value' => 'band',
'attr' => ['class' => 'py-2 px-4 ml-2 mt-1 border border-gray-300']
])
->add('mode', TextType::class, [
'attr' => ['class' => 'w-full py-2 px-4 ml-2 mt-1 border border-gray-300']
])
->add('frequency', NumberType::class, [
'scale' => 4,
'invalid_message' => 'The frequency is invalid.',
'attr' => ['class' => 'w-full py-2 px-4 ml-2 mt-1 border border-gray-300']
])
->add('comments', TextareaType::class, [
'attr' => ['class' => 'w-full py-2 px-4 ml-2 mt-1 border border-gray-300']
])
->add('submit', SubmitType::class, [
'attr' => ['class' => 'rounded-md font-bold border py-2 px-4 mx-auto bg-blue-500',
'onclick' => 'SubmitOpSession'],
])
;
}
Some fields omitted… The form is instantiated in a controller:
$form = $this->createForm(MyFormType::class, $ThisOpSession, [
'attr' => ['class' => 'w-full'],
'action' => $this->generateUrl('myendpoint'),
]);
$form->handleRequest($Request);
If it’s relevant, $Request
is an HttpFoundation
object.
The form displays fine on the initial page load. On submitting (POST) with form data, I’m getting:
App\Entity\OpSession::setBand(): Argument #1 ($band) must be of type string, App\Entity\Band given, called in /var/www/rtqm/vendor/symfony/property-access/PropertyAccessor.php on line 532
What I’m expecting to happen is that when submitted, handleRequest
should be loading the $ThisOpSession
object with the updated form data and what I read from the error message is that instead of passing the “band” choice from the form to the class property setter, handleRequest
is passing a Band entity.
The option values for the select are enumerated properly, and the POST
data is as I’d expect, so I’m not sure why handleRequest
is having issues with it.
What am I missing?
In the “what I’ve tried” category, I eliminated the EntityType
and passed an array of objects of type Band into the form as an option as so:
$BandRepo = $entityManager->getRepository(Band::class);
$Bands = $BandRepo->findAll();
$form = $this->createForm(OpSessionFormType::class, $ThisOpSession, [
'attr' => ['class' => 'w-full'],
'action' => $this->generateUrl('osmanager'),
'bands' => $Bands,
]);
And replaced EntityType
with ChoiceType
:
->add('band', ChoiceType::class, [
'choices' => $options['bands'],
'choice_label' => 'band',
'choice_value' => 'band',
])
Still getting the exact same error.