I have a Symfony 4 form where I'd like to manipulate a field's value.
I have more listeners attached to it: PRE_SET_DATA, PRE_SUBMIT and SUBMIT (last one for verification purposes only).
To be more precise, it's a file uploader field in a modification form, so if the user left empty, then I want to use the value was before (no changes).
$builder->addEventListener(FormEvents::PRE_SET_DATA, function (FormEvent $event) {
$data = $event->getData();
$event->getForm()->get('picture')->setData($data->getPicture());
in the example above, i've set the field's value with the stored one (from entity), that's working
$builder->addEventListener(FormEvents::PRE_SUBMIT, function (FormEvent $event) {
$data = $event->getData(); // array
$entity = $event->getForm()->getData();
if (empty($data['picture']) && !empty($entity)) {
$data['picture'] = $entity->getPicture();
$event->setData($data);
}
dumping $event still contains the data, that's fine so far.
$builder->addEventListener(FormEvents::SUBMIT, function (FormEvent $event) {
dump($event);
});
but the SUBMIT event forgets all, and at this point i'm unable to add it.
The goal is to set up picture field's value that goes towards the SUBMIT and not disappears.
question from:
https://stackoverflow.com/questions/65840980/missing-data-from-formevents-pre-submit-to-submit-listener 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…