2

When a form doesn't validate, I need to access the submitted data inside a Form Class in order I can set some options in a custom field.

I have tried with

$data = $builder->getForm()->getData();
$data = $builder->getData();

but $data has the empty object. So... what is the correct form to access the submitted data by the user after validation error in the form class?

Thanks

Gerardo
  • 5,800
  • 11
  • 66
  • 94
  • You could try get single field data by: `$fieldData = $builder->getForm()->get('field')->getData();`. But it's rather bad way. What about form events? It would be better solution : ) http://symfony.com/doc/current/cookbook/form/dynamic_form_modification.html – NHG Mar 07 '14 at 14:44
  • You could get just the request data: `$this->get('request')->request->get()`. See [this issue](http://stackoverflow.com/a/6917440/1847340) for more details. – ferdynator Mar 07 '14 at 14:46
  • I have tried Form Events but I can't find the way to achieve this. I need to add the field in the original form and, if the form does not validate, populate one field with a depending value from other field – Gerardo Mar 07 '14 at 15:02
  • Can you post some more details about your attempt with events? Because I was about to reply with an answer linking to the same page that NHG provided – Peter Bailey Mar 07 '14 at 17:35

3 Answers3

4

The problem is you're trying to access submitted data when it has not be handled yet. Basically, when you are in a builder (buildForm for the abstract types), you are building your form structure. It has nothing to do with form submission/binding. This is why you get the initial data when you call $builder->getData() because it only know the initial data at this state.

Knowing that the form component allows you to access the submitted data via events. You can attach a listener to your builder and rely on one of the *_submit event. The FormEvent class will given you the submitted data with $event->getData().

See this doc for more information: http://symfony.com/doc/current/cookbook/form/dynamic_form_modification.html

egeloen
  • 5,844
  • 1
  • 27
  • 38
0

Look into $options variable (var_dump it)

As I remeber you are looking for

$options['data']
Jekis
  • 4,274
  • 2
  • 36
  • 42
0

Using Form Events.

For those who wonder how Form Events are used.

Here is an example where you can modify the form after the user has tapped the submit button.

use Symfony\Component\Form\FormEvents;
use Symfony\Component\Form\FormEvent;
// ...

/* Listener to order to set a price if it does not exist yet */
$builder->get('price')->addEventListener(FormEvents::PRE_SUBMIT, function (FormEvent $event) {
    $data = $event->getData();

    // modify it as you wish

    $event->setData($data);
});

The FormEvents::PRE_SUBMIT event is dispatched at the beginning of the Form::submit() method.


If needed, here is an example where you can modify the form price before you display it.

use Symfony\Component\Form\FormEvents;
use Symfony\Component\Form\FormEvent;
// ...

/* Listener to order to set a price if it does not exist yet */
$builder->get('price')->addEventListener(FormEvents::PRE_SET_DATA, function (FormEvent $event) {
    $data = $event->getData();

    if (null === $data) { $data = '0.00'; }

    $event->setData($data);
});

The FormEvents::PRE_SET_DATA event is dispatched at the beginning of the Form::setData() method.

Mick
  • 30,759
  • 16
  • 111
  • 130