Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Form Collection error bubbling

Hello I have a problem with collection of text fields in my form. When there are errors in one of the fields, these errors bubbles into parent form so they are not assigned to field but to parent itself. It is the 'points' collection in the following piece of code. I tried to set error_bubbling to false, but it has no effect.

    <?php
    namespace JamaLvova\AdminBundle\Form\Type;

    use Symfony\Component\Form\AbstractType;
    use Symfony\Component\Form\FormBuilderInterface;
    use Symfony\Component\OptionsResolver\OptionsResolverInterface;
    use JamaLvova\AdminBundle\Form\Type\ExercisePointsFormType;

    class StartContestFormType extends AbstractType
    {
        public function buildForm(FormBuilderInterface $builder, array $options)
        {

            $builder->add('startYear', 'hidden')
                   /*
                       some other form elements
                   */
                    ->add('points', 'collection', array(
                        'type' => 'text',
                        'allow_add' => true,
                        'label' => 'Body za jednotlivé úlohy:',
                        'error_bubbling' => false,
                        'options' => array(
                            'error_bubbling' => false,
                            'attr' => array("maxlength" => "4", "size" => "4")
                        )
                        ));
        }

        public function setDefaultOptions(OptionsResolverInterface $resolver)
        {
            $resolver->setDefaults(array(
                'data_class' => 'JamaLvova\AdminBundle\Form\StartContestForm',
            ));
        }

        public function getName()
        {
            return 'startContestForm';
        }
    }

In StartContestForm I have $points property written like this:

     /**
     * @Assert\Type(type="integer", message="Hodnota {{ value }} není celé číslo.")
     * @Assert\Range(
     *      min = "0",
     *      max = "",
     *      minMessage = "Body nemohou být záporné",
     *      maxMessage = "Příliš mnoho bodů"
     * )
     */
     private $points;

In twig template when I iterate over form.points, no field has errors, but form.points does. Does anyone have any idea where problem could be? Or am I missing something? Thanks a lot :-) (Symfony v. 2.1.4)

EDIT: It seems that if I use collection of forms ('type' => new PointsFormType()) instead of that 'type' => 'text', it somehow works as expected. Does it mean that I always need to use collection of forms to be able to assign errors to certain field?

like image 531
Tomáš Koblížek Avatar asked Sep 02 '25 07:09

Tomáš Koblížek


1 Answers

You may need to add cascade_validation' => true

$builder->add('startYear', 'hidden')
       /*
           some other form elements
       */
        ->add('points', 'collection', array(
            'type' => 'text',
            'allow_add' => true,
            'label' => 'Body za jednotlivé úlohy:',
            'error_bubbling' => false,
            'cascade_validation' => true,
            'attr' => array("maxlength" => "4", "size" => "4")
        ));
}
like image 159
Leevi Graham Avatar answered Sep 05 '25 00:09

Leevi Graham