`FileField::setUploadedFileNamePattern()` with closure not working as expected

Open
#7,839 0 comments 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 2 days

Nobody has claimed this yet.

Assessment

Difficulty
4/5
Estimated time
3-5 days
Newbie friendliness
45/100
Issue type
Bug
Clarity
Mostly clear
Activity status
Active
Tech stack
php, symfony
Domain
backend

Research direction

Start by reproducing the create and edit cases described in the issue, then inspect StringToFileTransformer and the upload field's submission flow. Determine where the filename callback runs relative to parent-form mapping, and identify the relevant tests or entry points for that flow. Done means the callback can use the submitted year and produces filename-2037.pdf or filename-2038.pdf as appropriate.

Written by the indexing model from the issue text.

Description

Describe the bug

I want to generate an uploaded PDF’s filename using the event’s year entered in the same form.

However, the entity passed to the setUploadedFileNamePattern() callback does not yet contain the submitted values.

When creating an event with year = 2037, the callback sees year = null and generates filename-.pdf instead of filename-2037.pdf. The saved entity correctly contains 2037.

To Reproduce

Create an Event entity with a year property initially set to null, a getYear(): ?int getter, and a media property storing the uploaded filename.

Create a CRUD with these fields:

IntegerField::new('year', 'Year'),
FileField::new('media', 'File')
    ->hideOnIndex()
    ->setBasePath('uploads/events')
    ->setUploadDir('public/uploads/events')
    ->setUploadedFileNamePattern(
        fn (UploadedFile $file, Event $event): string =>
            'filename-'.$event->getYear().'.[extension]'
    )
    ->mimeTypes('.pdf'),

Create a new event, enter 2037 as the year, upload a PDF, and save.

Expected filename: filename-2037.pdf.
Actual filename: filename-.pdf.

(OPTIONAL) Additional context

EasyAdmin version: 5.6.1.

The issue also occurs when editing an event: changing the year from 2037 to 2038 and uploading another PDF generates filename-2037.pdf instead of filename-2038.pdf.

StringToFileTransformer invokes the filename callback during the upload field’s submission, before Symfony maps the parent form’s submitted values to the entity. Placing the year field before the upload field does not resolve the issue.

I am not sure if this is expected, but passing the entity before mapping is not really helpful.

Dominant language
PHP
Stars
4.3k
Forks
1.1k
Avg merge
2d 9h
Merged PRs (30d)
52

Getting set up

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from EasyCorp/EasyAdminBundle

All issues in EasyCorp/EasyAdminBundle

Similar issues

More PHP issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.