For example we have two entities one Car and one Person, when we modify PersonType like below:
$builder
->add('email')
->add('cars','collection',array(
'type' => new CarType(),
'allow_add' => true,
'allow_delete' => true,
'by_reference' => false,
))
->add('submit','submit')
and have following validation rules for Car entity:
Mtm\AppBundle\Entity\Car:
properties:
file:
- File:
maxSize: 100k
mimeTypes: [application/pdf, application/x-pdf]
mimeTypesMessage: Please upload a valid PDF
Validation rules doesn't apply on form, it just makes html5 validation on it, not server side validation.
Any idea how should I do that?
Set cascade_validation
property to true:
$builder
->add('email')
->add('cars','collection',array(
'type' => new CarType(),
'allow_add' => true,
'allow_delete' => true,
'by_reference' => false,
'cascade_validation' => true
))
->add('submit','submit')
http://symfony.com/doc/current/reference/forms/types/collection.html#cascade-validation
Working solution is below:
/**
* @param OptionsResolverInterface $resolver
*/
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$resolver->setDefaults(array(
'data_class' => 'Mtm\AppBundle\Entity\Person',
'cascade_validation' => true,
));
}
I had to add it in defaultOptions.
Thx to dmnptr for providing way to find the right answer.