4

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';
}
Artur Schroeder
  • 243
  • 4
  • 15

2 Answers2

4

I know I am late to this but what fixed my problem was i was missing the following inside form

enctype="multipart/form-data"

 <form method="post" action="{{ path('your_route_name') }}" novalidate enctype="multipart/form-data">
Shairyar
  • 3,268
  • 7
  • 46
  • 86
0

You should use different class properties for the file upload field and filepath storage. So one to process the file field (@Assert..) and one to store the path in the db (@ORM...)

From the documentation:

To handle the actual file upload in the form, use a "virtual" file field.

If you still have issues, try without Gedmo extension.