You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
class Item
{
public $title;
}
class Foo implements \IteratorAggregate
{
public $items;
public function __construct()
{
$this->items = [new Item()];
}
public function getIterator()
{
return new \ArrayIterator([
'items' => $this->items
]);
}
}
class ItemType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add('title', 'text');
}
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$resolver->setDefaults([
'data_class' => __NAMESPACE__ . '\Item',
'error_bubbling' => false
]);
}
public function getName()
{
return 'item';
}
}
class FooType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add('items', 'collection', [
'type' => new ItemType()
]);
}
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$resolver->setDefaults([
'data_class' => __NAMESPACE__ . '\Foo',
'error_bubbling' => false
]);
}
public function getName()
{
return 'foo';
}
}
The reason for your problem is that Foo implements \IteratorAggregate. Currently, the Form component enables object traversal for the data of the root form when launching the validation. Consequently, the generated violation has the path
data[items][0].title
instead of
data.items[0].title
To fix your problem, you can either remove the \IteratorAggregate interface from Foo or overwrite the property path for all fields in FooType:
See example of embedded form.
Resources/config/validation.yml:
Now when I call this
Error from items[0].title bubbles to the root of the form
The text was updated successfully, but these errors were encountered: