lundi 13 avril 2015

Symfony 2 form optional image

I have an issue with my Symfony 2 form and my image upload.


I created a form :



$form = $formFactory->create(new PlaceType($this->_em), $place, array(
'method' => 'POST',
'status' => $place->getStatus()
));


In PlaceType.php :



public function buildForm(FormBuilderInterface $builder, array $options) {
$builder->add('images', 'collection', array(
'type' => new ImageType($this->em),
'allow_add' => true,
'data' => array(new Image())));
}


In ImageType.php :



public function buildForm(FormBuilderInterface $builder, array $options) {
$builder->add('file', 'file', array('required' => false));
}


It's working very well, but when I don't fill the fiel field, I have an exception threw because file infos are missing.


I would like to make this field optional. I tried a lot of solutions, but none is working.


Thanks.


Aucun commentaire:

Enregistrer un commentaire