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

Aperta
#7,839 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub

I maintainer di solito rispondono entro 2 giorni

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
4/5
Tempo stimato
3-5 giorni
Idoneità per principianti
45/100
Tipo di issue
Bug
Chiarezza
Abbastanza chiara
Stato di attività
Attiva
Stack tecnologico
php, symfony
Ambito
backend

Direzione di ricerca

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.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

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.

Lingua principale
PHP
Stelle
4.3k
Fork
1.1k
Merge medio
2g 9h
PR unite (30g)
52

Preparare l'ambiente

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di EasyCorp/EasyAdminBundle

Tutte le issue di EasyCorp/EasyAdminBundle

Issue simili

Altre issue su PHP

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.