JsFormValidatorBundle
JsFormValidatorBundle copied to clipboard
Invalid ErrorPathElement
The Problem
I have the following form:
class ManoDeObraForm extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
..
->add('materiales', 'collection', array(
'type' => new ManoDeObraMaterialType(),
'allow_add' => true,
'allow_delete' => true,
)) ;
}
}
The Collection item form is:
class ManoDeObraMaterialType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('material', 'entity', array(
...
'error_bubbling' => true, // Important
))
->add('cantidad', 'text', array(
'error_bubbling' => true, // Important
));
}
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$resolver->setDefaults(array(
'error_bubbling' => false, // Important
));
}
The result expected is like:
But it is showing:
This is because the method FpJsFormValidator.getErrorPathElement
in javascript call internally to FpJsFormValidator.getRootElement
if element.bubbling
is set to true, and getRootElement return the root element of validation form object.
Solution
Replace:
this.getErrorPathElement = function (element) {
if (!element.bubbling) {
return element;
} else {
return this.getRootElement(element);
}
};
For
this.getErrorPathElement = function (element) {
if (element.bubbling && element.parent) {
return this.getErrorPathElement(element.parent);
} else {
return element;
}
};
Ping @jumale