Symfony2 - Validation not working for embedded Form Type

后端 未结 8 1415
北海茫月
北海茫月 2020-12-07 12:59

I have a form that combines two entities (User and Profile).

Validation seems to work on the first part of the form that comes form the User Entity and is the basis

8条回答
  •  没有蜡笔的小新
    2020-12-07 13:32

    I spent an age searching and found that it was adding 'cascade_validation' => true to the setDefaults() array in my parent type's class that fixed it (as mentioned already in the thread). This causes the entity constraint validation to trigger in the child types shown in the form. e.g.

    public function setDefaultOptions(OptionsResolverInterface $resolver)
    {
        $resolver->setDefaults(array(            
            ...
            'cascade_validation' => true,
        ));
    }
    

    For collections, also make sure to add 'cascade_validation' => true to the $options array for the collection field on the form. e.g.

    $builder->add('children', 'collection', array(
        'type'         => new ChildType(),
        'cascade_validation' => true,
    ));
    

    This will have the UniqueEntity validation take place as it should in the child entity used in the collection.

提交回复
热议问题