Accessing a form field from a subscriber (of a form event) in Symfony2

早过忘川 提交于 2019-12-06 13:26:35

I could be wrong about it this, but I don't believe you can change a form's attributes after its been created. However, you can add to the form.

Instead of adding the 'name' field in ProductType::buildForm, you can defer this to the subscriber:

if (!$data->getId()) {
    $form->add($this->factory->createNamed('text', 'name', null, array('required' => false)));
} else {
    $form->add($this->factory->createNamed('text', 'name'));
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!