doushi4795 2016-05-03 15:00
浏览 45

symfony FormType:EventListener中的setData

I've written a symfony FormType-Class which has a POST_SUBMIT EventListener, in which I wanted to set a default value for an existing field dependant on another, filled form element.

In other words: dependant on a selected, existing user (entity select) I want to set the default data of another entity-select field (country and state) to the correspondant value.

The code looks like this:

$formModifier = function (FormInterface $form, Person $person = null) 
{
    if ($person) {
        $form->get('country')->setData(
            $person->getCountry()
        );
    } 
};

By selecting the user the form is submitted but not successfully validated, so the Country should be selected. The Person-Object exists, the value is correct and the statement is reached.

But the value isn't selected. Isn't this possible by using a POST_SUBMIT-FormEvent?

Additional info: symfony 2.7, PHP 5.6, implementation done like the second example under http://symfony.com/doc/2.7/cookbook/form/dynamic_form_modification.html#dynamic-generation-for-submitted-forms

Thanks in advance!

Martin

Edit: The formModifier is called like this:

$builder->get('field')->addEventListener(
    FormEvents::POST_SUBMIT,
    function (FormEvent $event) use ($formModifier) {
        $person = $event->getForm()->getData();
        $formModifier($event->getForm()->getParent(), $person)
    }
};
  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 keil的map文件中Image component sizes各项意思
    • ¥30 BC260Y用MQTT向阿里云发布主题消息一直错误
    • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
    • ¥15 划分vlan后,链路不通了?
    • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
    • ¥15 Vue3 大型图片数据拖动排序
    • ¥15 Centos / PETGEM
    • ¥15 划分vlan后不通了
    • ¥20 用雷电模拟器安装百达屋apk一直闪退
    • ¥15 算能科技20240506咨询(拒绝大模型回答)