The FormBuilderInterface is a class in the Symfony Component Form library that provides a simple way of creating and managing forms in PHP. One of its key features is the ability to add event listeners, which are functions that are executed when a specific event occurs, such as when a form is submitted.
Example 1: Adding a listener to a form submit event
$formBuilder->addEventListener(FormEvents::POST_SUBMIT, function (FormEvent $event) { $form = $event->getForm(); $data = $form->getData(); // Do something with the form data });
In this example, we use the $formBuilder object to add an event listener to the POST_SUBMIT event. When the form is submitted, the event listener is called, and we can access the form data through the FormEvent object.
Example 2: Adding a listener to a form pre-set data event
$formBuilder->addEventListener(FormEvents::PRE_SET_DATA, function (FormEvent $event) { $form = $event->getForm(); $data = $event->getData(); // Modify the form based on the data });
In this example, we add an event listener to the PRE_SET_DATA event, which is triggered when the form data is set for the first time. We can use this event to modify the form based on the data that is being provided.
Overall, the FormBuilderInterface provides a powerful way to create and manage forms in Symfony using PHP code. It is a part of the Symfony Component Form library.
PHP Symfony\Component\Form FormBuilderInterface::addEventListener - 30 examples found. These are the top rated real world PHP examples of Symfony\Component\Form\FormBuilderInterface::addEventListener extracted from open source projects. You can rate examples to help us improve the quality of examples.