EasyCorp / EasyAdminBundle

EasyAdmin is a fast, beautiful and modern admin generator for Symfony applications.
MIT License
3.99k stars 1.01k forks source link

[ImageField] Support constraint validation #6258

Open Seb33300 opened 1 month ago

Seb33300 commented 1 month ago

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')),