dsf23223 2014-09-27 15:55
浏览 22

禁用PRE_SET_DATA symfony2上的表单

I'm working on a form extension that can disable some fields in form types when the underlying data is not new.I do this so I don't have to create seperate forms or event listeners for update/creation forms.i.e: I create the entity creation form and the extension disables the fields signified by an option if the underlying entity is not new.I bound an event listener to the PRE_SET_DATA event as such:

class RemoveFieldsExtension extends AbstractTypeExtension{
public function buildForm(FormBuilderInterface $builder, array $options)
{
    if (!$options['disable_on_update']) {
        return;
    }

    $isNewCallback = $options['disable_on_update_is_new'];
    $em = $this->em;
    $builder->addEventListener(FormEvents::PRE_SET_DATA, function(FormEvent $event) use ($em, $isNewCallback, $builder){
            if(is_bool($isNewCallback))
                return $isNewCallback;

            if(is_callable($isNewCallback)){
                $isNew = call_user_func($isNewCallback, $event->getData(), $em);
                if(!$isNew){//the check for resolving if this is an update form or creation form
                 //   $builder->setDisabled(true);
                    $form = $event->getForm();
                    $form->getConfig()->setDisabled(true);
                }
            }
        },
        -200
    );

}
}

the extension works fine the problem is that the above code will produce 'FormConfigBuilder methods cannot be accessed anymore once the builder is turned into a FormConfigInterface instance.'
it also didn't work when I tried $builder->setDisabled(true);.what can I do to accomplish this?

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
    • ¥15 CSAPPattacklab
    • ¥15 一直显示正在等待HID—ISP
    • ¥15 Python turtle 画图
    • ¥15 关于大棚监测的pcb板设计
    • ¥15 stm32开发clion时遇到的编译问题
    • ¥15 lna设计 源简并电感型共源放大器
    • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
    • ¥15 Vue3地图和异步函数使用
    • ¥15 C++ yoloV5改写遇到的问题