简体   繁体   中英

Symfony 2 : access to form config and submitted data in PRE_SUBMIT event

I'm currently trying to access to my form config (and its options) in a PRE_SUBMIT FormEvent callback. However, when I'm doing that for example :

<?php
// This is my callback function for the PRE_SUBMIT event on a formtype element
public function preSubmit(FormEvent $event)
{
    $data = $event->getData();
    $form = $event->getForm();
    [...]
    $myConfig = $form->get('my_form_element_child')->getConfig()->get('my_option');
?>

It raises an exception saying :

FormBuilder methods cannot be accessed anymore once the builder is turned into a FormConfigInterface instance.

Actually, I can understand why : it is because the form data is going to be submitted. However, it is a problem for me. Indeed, I need to find a way to create other fields in my form based on some config options, when the data is already entered by the user but before the data gets "handled" by the controller.

Any idea on how to do that?

Thank you !

Just replace

$myConfig = $form->get('my_form_element_child')->getConfig()->get('my_option');

to

$myConfig = $form->get('my_form_element_child')->getConfig()->getOption('my_option');

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM