Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[ImageField] Support constraint validation #6258

Merged
merged 1 commit into from May 21, 2024

Conversation

Seb33300
Copy link
Contributor

@Seb33300 Seb33300 commented Apr 13, 2024

Fixes #5227 and #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')),

@psihius
Copy link
Contributor

psihius commented May 21, 2024

Great PR.
This also needs to be reflected in the documentation on the SF site, who's able to update the docs for that? :)

@Seb33300
Copy link
Contributor Author

@psihius good point.

Just pushed a commit to update the doc.

@javiereguiluz javiereguiluz added this to the 4.x milestone May 21, 2024
@javiereguiluz
Copy link
Collaborator

Indeed, this is a very nice contribution. Thanks a lot Sébastien!

@javiereguiluz javiereguiluz merged commit 1fdacd6 into EasyCorp:4.x May 21, 2024
10 of 11 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

ImageField: bug with Symfony Image constraint
3 participants