Entity have nullable=true. Form have required = false. HTML generated havent any required.
When I leave file input I got error:
The file could not be found.
to be honest I have no idea where should I looking for... ;)
public function updateAction(Request $request, $id)
{
$em = $this->getDoctrine()->getManager();
$entity = $em->getRepository('FortiCoreBundle:Article')->find($id);
if (!$entity) {
throw $this->createNotFoundException('Unable to find Article entity.');
}
$deleteForm = $this->createDeleteForm($id);
$editForm = $this->createEditForm($entity);
$editForm->handleRequest($request);
// $editForm->isVail() false because have error The file could not be found.
if ($editForm->isValid()) {
Entity file:
/**
* @var string
*
* @ORM\Column(name="path", type="string", length=255, nullable=true)
* @Gedmo\UploadableFilePath
*/
private $path;
/**
* @var string
*
* @Assert\File(
* maxSize="3M",
* mimeTypes={"image/png", "image/jpeg", "image/pjpeg"}
* )
*
* @ORM\Column(name="filename", type="string", nullable=true)
* @Gedmo\UploadableFileName
*/
private $filename;
public function uploadPath()
{
return 'images/article/' . date('Y') . '/' . date('m');
}
public function getRelativePath()
{
if (empty($this->filename)) {
return null;
} else {
return $this->uploadPath() . '/' . $this->filename;
}
}
/**
* Set filename
*
* @param string $filename
* @return Article
*/
public function setFilename($filename)
{
if (!is_null($filename)) {
$this->filename = $filename;
}
return $this;
}
/**
* Get filename
*
* @return string
*/
public function getFilename()
{
return $this->filename;
}
and FormType file:
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
//...
->add('filename', 'file', array(
'label' => 'Zdjęcie',
'required' => false,
'data_class' => null,
'attr' => array(
"class" => 'no-border btn-xs')
))
//...
;
}
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$resolver->setDefaults(array(
'data_class' => 'Forti\CoreBundle\Entity\Article'
));
}
public function getName()
{
return 'forti_corebundle_articletype';
}