symfony CollectionType类型字段子表单字段移除

工作中我们经常会遇到子表单字段移除工作,哪怎么移除提不想要的子表单字段呢。代码如下

 1     public function buildForm(FormBuilderInterface $builder, array $options)
 2     {
 3         $builder
 4             ->add('stocks', CollectionType::class, [
 5                 'label'         => false,
 6                 'entry_type'    => ProductStockType::class,
 7                 'entry_options' => [
 8                     'label' => false,
 9                 ],
10                 'allow_delete'  => true,
11             ])
12         ;
13 
14         $builder->get('stocks')->addEventListener(FormEvents::PRE_SET_DATA, function(FormEvent $event){
15             foreach ($event->getForm()->all() as $f)
16                 $f->remove('status');
17         });
18     }
原文地址:https://www.cnblogs.com/mickeyooo/p/9417433.html