-
-
Notifications
You must be signed in to change notification settings - Fork 9.8k
Description
The form doesn't validate properly while using by_reference as false.
Let's assume a trivial one to many association such as product and feature and their related form and collection types. Feature has a non blank field called "name".
Let's add now two elements in the collection and remove the first one.
Form submission is prevented by validation but the validation error doesn't show up.
The current behaviour is shown in the left, while in the right the expected behaviour (with by_reference true).
The issue shown in the Symfony Profiler is pretty straightforward, the collection created (cloned?) has different indexes compared to the one held by the form, then the field doesn't get decorated with its error.
data.features[0].name | This value should not be blank. | null | ConstraintViolation
The one held by the form is data.features[1].name.
As a workaround I regenerated indexes on the form side during the PRE_SUBMIT event.
->addEventListener(FormEvents::PRE_SUBMIT, function (FormEvent $event) {
$data = $event->getData();
if(isset($data['features'])) {
$data['features'] = array_values($data['features']);
}
$event->setData($data);
})
The form should exhibit the same behaviour in both cases (by_reference true or false).
To replicate the issue, it is enough to copy and paste the example in the documentation here.
In the Javascript part this sentence:
$collectionHolder.data('index', $collectionHolder.find(':input').length);
had to be corrected in:
$collectionHolder.data('index', $collectionHolder.find(':input').length-1);
to work properly, since the former takes into consideration the add button.

