M
M
Maxim Lagoysky2019-07-31 11:01:21
symfony
Maxim Lagoysky, 2019-07-31 11:01:21

How to return data from form event?

Good afternoon everyone, tell me such a moment in the forms, I can’t figure it out myself. I have a select on the form, when I select anything there, ajax happens and the PRE_SET_DATA event occurs. Here the first moment arises, in the documentation the data is obtained as follows

$builder->addEventListener(
            FormEvents::PRE_SET_DATA,
            function (FormEvent $event) {
                $form = $event->getForm();
                $data = $event->getData();
                .....

according to the idea, there should be data in $data, but naturally they are not there because this event fires before the data is set, and the data is only in $_POST, although the event says that it is in this event that new fields should be added if necessary, there is one question, how to get the value of the selected field from? take from $_POST or what?
The second question, when I made a choice in the select, I make a request to the database and get the data, and I just need to insert one of the values ​​into the span, how can this be done?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Antony Tkachenko, 2019-08-01
@LemonFox

https://symfony.com/doc/current/form/dynamic_form_...
https://github.com/symfony/symfony/blob/4.3/src/Sy...
Model data is passed to the event. What makes you think that there is nothing there?
Or you forms are used without data_class?
If you need to use values ​​from a request in an event, create an EventListener and use this to inject request - https://symfony.com/doc/current/service_container/...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question