26

This is the FormEvents class from Symfony2 repository on github. It's linked from the main article, How to Dynamically Generate Forms Using Form Events.

Anyone konws exactly when these events are called in the flow?

namespace Symfony\Component\Form;

/**
 * @author Bernhard Schussek <bernhard.schussek@symfony.com>
 */
final class FormEvents
{
    const PRE_BIND = 'form.pre_bind';
    const POST_BIND = 'form.post_bind';
    const PRE_SET_DATA = 'form.pre_set_data';
    const POST_SET_DATA = 'form.post_set_data';
    const BIND_CLIENT_DATA = 'form.bind_client_data';
    const BIND_NORM_DATA = 'form.bind_norm_data';
    const SET_DATA = 'form.set_data';
}
j0k
  • 21,914
  • 28
  • 75
  • 84
Polmonino
  • 3,422
  • 8
  • 34
  • 52

1 Answers1

66

There are two types of events:

DataEvent - read-only access to the form data. 'Pre' and 'Post' events are read-only.

FilterDataEvent - event that allows the form data to be modified.

form.pre_bind DataEvent triggered before data is bound to the form. Triggered by Symfony\Component\Form\Form::bind()

form.post_bind DataEvent triggered after data is bound to the form. Triggered by Symfony\Component\Form\Form::bind()

form.pre_set_data DataEvent triggered before fields are filled with default data. Triggered by Symfony\Component\Form\Form::setData()

form.post_set_data DataEvent triggered after fields are filled with default data. Triggered by Symfony\Component\Form\Form::setData()

form.bind_client_data FilterDataEvent triggered before data is bound to the form. Triggered by Symfony\Component\Form\Form::bind()

form.bind_norm_data FilterDataEvent triggered after data has been normalized. Triggered by Symfony\Component\Form\Form::bind(). See Symfony\Component\Form\Extension\Core\EventListener\FixUrlProtocolListener (added by the UrlType for an example)

form.set_data FilterDataEvent triggered while default data is being bound. Triggered by Symfony\Component\Form\Form::setData()

I'd recommend poking around the Form class itself to get a better feel for when these events are triggered, and how you can use them.

xanido
  • 1,231
  • 11
  • 10
  • Really thank you, answer accepted! In the meanwhile i'm having some problems with form events, if you have some spare time maybe you can give me a little help: http://stackoverflow.com/questions/9661026/accessing-a-form-field-from-a-subscriber-of-a-form-event-in-symfony2 – Polmonino Mar 12 '12 at 02:27
  • 14
    It should be noted this description of the form events is only valid for Symfony 2.0 Form Component. The following events have been deprecated as of 2.1 and will be removed as of 2.3: `form.bind_client_data`, `form.bind_norm_data`, `form.set_data`. As of 2.1, form event listeners now receive a `FormEvent` object instead of the deprecated `FilterDataEvent` and `DataEvent` objects, which will be removed in 2.3. – John Kary Nov 29 '12 at 06:34
  • Hey @xanido, your links are broken. – Mick May 20 '13 at 11:42
  • Links updated to point to 2.0 source (which as has been mentioned is the only version this answer really applies to). Thanks @Patt – xanido May 22 '13 at 07:56