如何保存表单集合(CollectionType)

时间:2019-06-17 17:02:04

标签: rest symfony

我要保存收集表格。 我有一个基本表单BasePromotionsType,我想在其中保存另一个表单NumberOfVisitsType。但是我对This form should not contain extra fields出错。在解包程序中,在字段number_of_visits中有一个数组供我使用: array:2 [▼ "count_visits" => 10 "count_points" => 100 ]

要填写表格,我提交以下json:

{
  "active": true,
  "use_promotion": true,
  "label": "string",
  "description": "string",
  "picture": 1,
  "available_cities": [
    1
  ],
  "promotions_settings": 1,
  "list_users": [

  ],
  "number_of_visits": {
    "count_visits": 10,
    "count_points": 12
  }
}
public function buildForm(FormBuilderInterface $builder, array $options)
   {

       $builder
           ->add('active', CheckboxType::class, ['label' => 'active', 'required' => false])
           ->add('use_promotion', CheckboxType::class, ['label' => 'Одноразовая/Многоразовая', 'required' => false])
           ->add('label', TextType::class, ['label' => 'Заголовок', 'required' => false])
           ->add('description', TextType::class, ['label' => 'Описание', 'required' => false])
           ->add('picture', IntegerType::class, [
               'invalid_message' => 'avatar is not a valid',
           ])
           ->add('available_cities', CollectionType::class, [
               'entry_type' => IntegerType::class,
               'allow_add' => true,
               'by_reference' => false,
               'allow_delete' => true,
               'prototype' => true,
               'invalid_message' => 'cities is not a valid',
           ])
           ->add('promotions_settings', IntegerType::class, [
               'invalid_message' => 'settings are invalid',
           ])
           ->add('list_users', CollectionType::class, [
               'entry_type' => IntegerType::class,
               'allow_add' => true,
               'by_reference' => false,
               'allow_delete' => true,
               'prototype' => true,
               'invalid_message' => 'cities is not a valid',
           ])
           ->add('number_of_visits', CollectionType::class, [
               'entry_type' => NumberOfVisitsType::class
           ])
           ->add('send', SubmitType::class, ['label' => 'Отправить'])
       ;

       $builder->get('picture')
           ->addModelTransformer($this->avatarTransformer);
       $builder->get('available_cities')
           ->addModelTransformer($this->citiesTransformer);
       $builder->get('promotions_settings')
           ->addModelTransformer($this->promotionsSettingsTransformer);
       $builder->get('list_users')
           ->addModelTransformer($this->usersTransformer);
   }

因此,我需要保留基本表单以及该基本表单中内置的表单。

1 个答案:

答案 0 :(得分:0)

我解决了这个问题,有必要在访问次数

字段中注册
->add('number_of_visits', CollectionType::class, [
                'entry_type' => NumberOfVisitsType::class,
                'invalid_message' => 'number are invalid',
                'allow_add' => true,
                'allow_extra_fields' => true
            ])