I have a form with a file field, I'd like to have this field mandatory only when I create the record, not on update. in buildForm I only have this for the field:
->add('file', 'file', array(
'required' => false,
))
and in the controller I check for the id to decide if insert or update
thanks
In Your form class add:
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$resolver->setRequired([
'update',
]);
}
Then, when you are creating form use this:
$form = $this->createForm('formName', $object, array(
'update' => $entity->getId==null?false:true,
));
And after that, in your form, in $options array you can use $options['update']. Eg.:
->add('file', 'file', array(
'required' => !$options['update'],
))
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments