This assert is passing Symfony's form validation when uploading any file with VichUploaderBundle:
/**
* @Vich\UploadableField(mapping="product_media", fileNameProperty="path")
* @Assert\File(
* mimeTypes = {"image/jpeg", "image/gif", "image/png", "video/mp4", "video/quicktime", "video/avi"},
* mimeTypesMessage = "Wrong file type (jpg,gif,png,mp4,mov,avi)"
* )
* @var File $pathFile
*/
protected $pathFile;
I cannot see what the problem is with the assert. How can I validate file types with VichUploader?
You can use validation callback to solve this issue.
/**
* @ORM\Entity(repositoryClass="AppBundle\Entity\Repository\EntityRepository")
* @ORM\Table(name="entity")
* @Assert\Callback(methods={"validate"})
* @Vich\Uploadable
*/
class Entity
{
/**
* @Assert\File(maxSize="10M")
* @Vich\UploadableField(mapping="files", fileNameProperty="fileName")
*
* @var File $file
*/
protected $file;
/**
* @ORM\Column(type="string", length=255, name="file_name", nullable=true)
*
* @var string $fileName
*/
protected $fileName;
...
/**
* @param ExecutionContextInterface $context
*/
public function validate(ExecutionContextInterface $context)
{
if (! in_array($this->file->getMimeType(), array(
'image/jpeg',
'image/gif',
'image/png',
'video/mp4',
'video/quicktime',
'video/avi',
))) {
$context
->buildViolation('Wrong file type (jpg,gif,png,mp4,mov,avi)')
->atPath('fileName')
->addViolation()
;
}
}
}
For Symfony 4.0 you'll need to import the Validator component
composer require validator
Now in your Entity class you can use the @Assert annotation.
// src/Entity/Author.php
// ...
use Symfony\Component\Validator\Constraints as Assert;
class Author
{
/**
* @Assert\NotBlank()
*/
public $name;
}
You might need to add some configuration in your config/packages/framework.yaml file. Anyway, all this is perfectly explained on the official Symfony documentation.
http://symfony.com/doc/current/validation.html
To check the mime type you'll need to use the File constraint http://symfony.com/doc/current/reference/constraints/File.html
Here is a working exemple
/**
* @ORM\Column(type="string", length=255)
* @var string
*/
private $cvFilename;
/**
* @Assert\File(
* maxSize = "2048k",
* mimeTypes = {"application/pdf", "application/x-pdf"},
* mimeTypesMessage = "Please upload a valid PDF"
* )
* @Vich\UploadableField(mapping="cv", fileNameProperty="cvFilename")
* @var File
*/
private $cvFile;
Now it's true that there is a mime and size option inside the @Vich\UploadableField anotation as described here https://github.com/dustin10/VichUploaderBundle/blob/master/Resources/doc/usage.md#step-2-link-the-upload-mapping-to-an-entity but I couldn't get this to work.
The @Assert annotation will generate Forms errors, that you can retrieve them in Twig to give a feedback.
The key is to use : form_errors(candidature_form.cvFile)
here is a working example :
{% set error_flag = form_errors(candidature_form.cvFile) %}
<label class=" {% if error_flag %}has-error{% endif %}">
Curriculum Vitae (PDF)
</label>
{{ form_widget(candidature_form.cvFile) }}
{% if error_flag %}
<div class="has-error">
{{ form_errors(candidature_form.cvFile) }}
</div>
{% endif %}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With