Child types are not checked using isValid () in symfony2

I have a problem regarding the validation of form children. I have a type that contains several types of another type. This subtype requires one field, one of which is not required. But both of them are empty, the form is verified. Here are my classes

class PassengerList { /** * @Assert\Valid() //If it an array of objet, should validate all the object of the list *traverse */ protected $passengers; /** * @Assert\Valid */ protected $passengers_child; /** * @Assert\Valid */ protected $passengers_baby; } 

passengerListType

 class PassengerListType extends AbstractType { public function buildForm(FormBuilder $builder, array $options) { $builder->add('passengers','collection', array( 'type' => new PassengerType(), 'allow_add' => false, 'required'=> true, 'allow_delete' => false)); $builder->add('passengers_child','collection', array( 'type' => new PassengerChildType(), 'allow_add' => true, 'allow_delete' => false)); $builder->add('passengers_baby','collection', array( 'type' => new PassengerBabyType(), 'allow_add' => true, 'allow_delete' => false)); } function getName() { return 'passenger_list'; } } 

PassengerType

 class PassengerType extends AbstractType { public function buildForm(FormBuilder $builder, array $options) { $date_params = array("widget"=>"single_text","input"=>"datetime",'format' => 'dd-MM-yyyy',); $builder ->add('civility', 'choice', array("choices"=>Passenger::getCivilityList())) ->add('birthdate','date',$date_params) ->add('last_name','text',array('required'=>true)) ->add('first_name','text',array('required'=>false)) ->add('type','hidden'); } function getName() { return 'passenger'; } public function getDefaultOptions(array $options) { return array( 'data_class' => 'Travelyo\CoreBundle\Entity\Passenger', ); } } 

which give me this form:

enter image description here

But when validating a form with $ form-> isValid (), the return value is true, although I leave both fields blank.

I have the feeling that the problem is that nothing is being tested on chidlren types.

Do you have any sheets that can help me?

+4
source share
1 answer

Are you using 2.1?

You need to specify validation groups or enable cascade_validation , similar to this question .

+4
source

All Articles