symfony CollectionType類型字段子表單字段移除

工做中咱們常常會遇到子表單字段移除工做,哪怎麼移除提不想要的子表單字段呢。代碼以下ui

 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     }
相關文章
相關標籤/搜索