EasyAdminBundle icon indicating copy to clipboard operation
EasyAdminBundle copied to clipboard

[ImageField] Support constraint validation

Open Seb33300 opened this issue 1 year ago • 0 comments

Fixes https://github.com/EasyCorp/EasyAdminBundle/issues/5227 and https://github.com/EasyCorp/EasyAdminBundle/issues/4088

Currently, it is not possible to properly validate images uploaded with the ImageField using Symfony constraints. This is because the constraints option is applied to both the parent field (returning a string) and the underlying FileType field (returning an UploadedFile) resulting in unexpected validation errors.

This PR adds a new setFileConstraints method to the ImageField to apply constraints to the FileType field only. By default, I also applied the Image constraint on the ImageField.

Usage example:

use Symfony\Component\Validator\Constraints\Image;

// Validate that file is an image by default
ImageField::new('logo', 'Logo'),

// Set custom validation constraints
ImageField::new('logo', 'Logo')->setFileConstraints(new Image(maxSize: '100k')),

Seb33300 avatar Apr 13 '24 14:04 Seb33300