I'm trying to submit array with values to a symfony 4 form field but the validation keeps failing.
I'm in the process of updating my application from symfony 2.7 to symfony 4. The problem is that a form that I used to use now always fails validation due to changes in symfony forms.
The symfony form has the following field
$builder->add('contactData', null, ['mapped' => false])
In symfony 2.7 I would always submit a POST request with array values in the contactData field and since it's not mapped it would just set the data to the field object in the submit process and the values were accessed in the Handler. Example request:
{
"name": {
"aField": "aValue",
"contactData": {
"something": "value"
}
}
}
However in symfony 4 there is now an added validation check in the \Symfony\Component\Form\Form class
} elseif (\is_array($submittedData) && !$this->config->getCompound() && !$this->config->hasOption('multiple')) {
that causes the validation to fail when submiting data to the contactData field, since the submittedData is indeed an array. I've been looking all over the internet and reading through the documentation of symfony but I can't seem to find a way to induce the same behavior as in symfony 2.7.
I would much appreciate any advice, I've been stuck on this for a while
Symfony has changed from v2.7 to 4.0, there is a lot of default values changed;
I faced the same problem and after 2 hours of investigations, I ended up adding the attributes
compoundandallow_extra_field.So, this should solve your problem:
EDIT:
This didn't work as expected, I ended up with no error and no content as a submitted data, so I created a new type to add fields dynamically on a pre-submit event as following:
UnstructuredType.php