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 %}