- Sponsor
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Closed
Description
Describe the bug
Hi, I'm using EasyAdmin on a side project, and I decided to use an ImageField on a form to upload... well, an image. That being done, I tried to use it and it was working just fine, until I tried to put a constraint on the ImageField in the CrudController. Once you put on constraint on it, to prevent users to from uploading other types of files, you get the error 'The file could not be found.'. It is even impossible to create the entity.
To Reproduce
- Create an entity with an Image property (string) and make a CRUD controller for it.
- In the CrudController, try to use the Image constraint from Symfony.
(OPTIONAL) Additional context
This is what I've done:
ImageField::new('image')
->setBasePath('uploads/post-images')
->setUploadDir('public/uploads/post-images')
->setUploadedFileNamePattern('[slug]-[timestamp].[extension]')
->setFormTypeOptions([
'required' => false,
'constraints' => [
new Image(),
],
]),
I think using the ImageField should be enough. We shouldn't have to add an Image constraint, should we?
Metadata
Metadata
Assignees
Labels
Type
Projects
Milestone
Relationships
Development
Select code repository
Activity
bocharsky-bw commentedon May 13, 2022
Well explained! Based on this, I think this issue has 2 sub-issues:
Image
constraint to add more strict limitations for specific casesDigi92 commentedon Jun 27, 2022
Hi,
in case someone else tries to add a mime validation to the field type "ImageField".
The main problem is that the field type "ImageField" only passes the file name as a value to the validator instead of the object.
Therefore I created my own constraint which first loads the object out of the context and then passes it to the Symfony validator.
With this it works for me now.
Versions:
Symfony: 6.1
Eeasyadmin: 4.3.2
CrudController
src/Validator/Constraint/EasyAdminFile.php
src/Validator/Constraint/EasyAdminFileValidator.php
bocharsky-bw commentedon Jun 28, 2022
I still think it should work out-of-the-box in EA, but your workaround is valid, thanks for sharing it!
Snowbaha commentedon Jun 30, 2022
@javiereguiluz It will be great to add the validation of Image with ImageField :)
bakhtiyor commentedon Jun 30, 2022
@Digi92 thanks for your implementation. I am getting following error message on using it, do you have any idea what is wrong?
abouross commentedon Aug 12, 2022
In fact currently it sends two values including the file name first then the file second, just ignore the validation if it is not an instance of FileUploaded:
src/Validator/EasyadminImage.php :
src/Validator/EasyadminImageValidator.php :
usage example :
COil commentedon Jan 18, 2023
I came to the same conclusion @abouross.
luismisanchez commentedon May 7, 2023
#5227 (comment)
Just to say thanks to @Digi92 . Working solution on Symfony 5.4.22 and Easyadmin 4.6.1.
psihius commentedon Feb 7, 2024
@javiereguiluz do you have any ideas on how this one could be fixed so we do not need a workaround? I plan to try and solve this issue, so looking for some feedback if there's one before I dive into this.
Seb33300 commentedon Apr 13, 2024
This is because the same constraints are passed to the
ImageField
form type (which is a string saved in the entity) and to the underlyingFileType
.In order to properly fix this issue, we should create a new custom option, for instance
file_constraints
that should be applied to the FileType only.Seb33300 commentedon Apr 13, 2024
I just created a PR to fix this issue: #6258
If you can, please try it!
ImageField
missing validation? #4088feature EasyCorp#6258 [ImageField] Support constraint validation (Seb…