EasyAdminBundle: Cannot modify entity with required file field from VichUploaderBundle
Hello,
I have Page entity class which has, among others, these properties:
class Page
{
// ...
/**
* @ORM\Column(type="string", length=255)
* @var string
*/
private $image;
/**
* @Vich\UploadableField(mapping="product_images", fileNameProperty="image")
* @var File
* @Assert\File()
* @Assert\NotBlank()
*/
private $imageFile;
// ...
}
Everything is all right when I’m creating new entity trough EasyAdmin panel. The problem starts when I want to edit something from this particular entity without changing the $image property. When I’m clicking submit button, error occurs:

How to make $image file property as “required” while allowing it to edit entity when the file is uploaded earlier?
About this issue
- Original URL
- State: closed
- Created 8 years ago
- Comments: 17 (7 by maintainers)
@javiereguiluz
Thanks to @laurent-bientz and its solution for image validation, I propose that we close this issue (and remove the
bugandunconfirmedlabels).Here is a summary for image validation when the image is mandatory (non-nullable).
Considering the following entity (getters and setters are not given):
Config file + front validation :
Back validation:
First, a quick comment: you should enable the translator service in Symfony. See that buttons and links display weird strings like
action.saveinstead ofSave. Just uncomment this line inapp/config/config.yml:The situation appears when a persistant non-nullable field (eg “photo”) is binded on a virtual field for the file (eg photoFile) with an Assert\NotBlank().
When we try to edit the recordset (no problem when adding), the assert on the file forces user to reupload the file, even if the field photo is set (in front with HTML validation and in back with assert on file), so the validation fails.
Putting a validation group on each entity with a non-nullable file is a pain.
Don’t know if it’s the best way but I solve the problem with an expression assert which checks both fields instead of only the virtual field:
and don’t forget to disable the allow_delete on the field, cause nonsense to allow delete on a non-nullable field:
Hope it helps, if someone have a best idea, I’ll take it 😉