Skip to content

ImageField missing validation? #4088

@lauraseidler

Description

@lauraseidler

Describe the bug
I'm using the ImageField like so:

ImageField::new('avatar')
    ->setUploadDir('public/uploads/avatars/')
    ->setBasePath('uploads/avatars')
    ->setUploadedFileNamePattern('[year]-[month]-[day]-[contenthash].[extension]')

This works, however I'm able to also upload files that are not images. I would be fine adding this validation manually, but I haven't really found a way to do so

To Reproduce
Install EA 3.2.0, use an ImageField like described above, and upload a file that's not an image

Activity

parijke

parijke commented on Dec 30, 2020

@parijke
Contributor

Do you check the mimetype?

lauraseidler

lauraseidler commented on Jan 4, 2021

@lauraseidler
Author

How would I go about this? I tried adding constraints to my entity, but I could not make File or Image constraints work, since the field only holds the file path it seems.

cristobal85

cristobal85 commented on Jan 5, 2021

@cristobal85

You can do this with these annotations in yours entities:

  • For 1 image:
   /**
     * @ORM\Column(type="string", length=255, nullable=true)
     * @Assert\Regex(
     *     pattern="/^.*\.(jpg|jpeg|png|gif)$/i",
     *     match=true,
     *     message="............"
     *   )
     */
  • For several images:
    /**
     * @ORM\Column(type="array", nullable=true)
     * @Assert\All({
     *   @Assert\Regex(
     *     pattern="/^.*\.(jpg|jpeg|png|gif)$/i",
     *     match=true,
     *     message="........................................"
     *   )
     * })
     */
parijke

parijke commented on Jan 5, 2021

@parijke
Contributor

I have this (but using vich)

    /**
     * @Vich\UploadableField(mapping="policy_pdf", fileNameProperty="policy", mimeType="mimeType")
     * @Assert\File(
     *     mimeTypes = {"application/pdf"},
     *     mimeTypesMessage = "Only PDF's can be uploaded"
     * )
     * @var File
     */
parijke

parijke commented on Jan 5, 2021

@parijke
Contributor

and my field def:

        $policyFile = Field::new('policyFile')
            ->setFormType(VichFileType::class);
lauraseidler

lauraseidler commented on Feb 6, 2021

@lauraseidler
Author

Both very helpful, thanks (apologies for the delayed response, I had to table this project for a while)!

I was indeed already using Vich before, so I don't really mind to keep using it, as I found it to work well for me, and I got it to work for single images just fine. Any ideas on how to make it work for multiple images @parijke? I'm thinking I might need to go the extra step here and do an Images entity, and then just associate them.

Seb33300

Seb33300 commented on Apr 13, 2024

@Seb33300
Contributor

Same as #5227
I created a PR here: #6258

added a commit that references this issue on May 21, 2024
javiereguiluz

javiereguiluz commented on May 21, 2024

@javiereguiluz
Collaborator

Closing as fixed in #6258.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @javiereguiluz@Seb33300@parijke@cristobal85@lauraseidler

        Issue actions

          `ImageField` missing validation? · Issue #4088 · EasyCorp/EasyAdminBundle