I am building a Symfony data mapper class called EventDataMapper that takes two TextType form fields and converts them into an EventDetails object stored by Doctrine. That EventDetails object is, in turn, referred to in an Event object. (The DataMapper also does some other things.) A slightly simplified version of my MapFormsToData() method looks like this:
public function mapFormsToData($forms, &$data): void {
...
foreach ($forms as $element => $form) {
switch ($form->getConfig()->getName()) {
case 'title':
$this->mapTitleFormToData($form, $element, $data);
$form->getParent()->remove('title');
break;
case 'description':
$this->mapDescriptionFormToData($form, $element, $data);
$form->getParent()->remove('description');
break;
}
}
$this->mapTitleAndDescriptionFieldsToEventDetailsData($data, $forms);
}
... and a slightly simplified version of my EventFormType's buildForm() method looks like this:
public function buildForm(FormBuilderInterface $builder, array $options): void
{
...
$builder
->add('title', TextType::class)
->add('description', TextareaType::class)
->setDataMapper(new EventDataMapper)
;
}
When I submit my form, however, I get a NoSuchPropertyException stating the following:
Could not determine access type for property "title" in class "AppBundle\Entity\Event": Neither the property "title" nor one of the methods "addTitle()"/"removeTitle()", "setTitle()", "title()", "__set()" or "__call()" exist and have public access in class "AppBundle\Entity\Event"..
As you can see, I have tried using the remove() method to take the form fields out of the form in order to avoid this exception. That, however, does nothing, which is not surprising, since the list of forms is being passed by value and not by reference. (Changing the function to pass by reference results in a FatalError as the method signature then no longer matches Symfony's DataMapperInterface.)
For reasons I won't get into here, I can't use a new form type to handle EventDetails, so an EventDetailsFormType is not an option. I must do this with my EventFormType class and my EventDataMapper class, and with those classes alone. Similarly, I must keep these two TextType fields in place with the names they currently have, in order not to disrupt work by other members of my team.
Is there a simple way to fix this within these constraints?
Found it!
I just had to add
['mapped' => false]to the form field definitions:Nice and easy.