php - 创建一个以 CollectionType 为父的自定义字段类型
问题描述
我有一个SmsType
,EmailType
并且VoiceCallType
全部包含以下集合:
->add('answers', CollectionType::class, [
'label' => 'form.communication.fields.answers',
'entry_type' => AnswerType::class,
'entry_options' => [
'label' => false,
],
'allow_add' => true,
'allow_delete' => true,
'delete_empty' => true,
'prototype' => true,
'required' => false,
'attr' => [
'class' => 'collection',
],
])
为了简化,我创建了一个AnswersType
作为CollectionType
父对象:
<?php
namespace App\Form\Type;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\CollectionType;
use Symfony\Component\OptionsResolver\OptionsResolver;
class AnswersType extends AbstractType
{
public function setDefaultOptions(OptionsResolver $resolver)
{
$resolver->setDefaults([
'label' => 'form.communication.fields.answers',
'entry_type' => AnswerType::class,
'entry_options' => [
'label' => false,
],
'allow_add' => true,
'allow_delete' => true,
'delete_empty' => true,
'prototype' => true,
'required' => false,
'attr' => [
'class' => 'collection',
],
]);
}
public function getParent()
{
return CollectionType::class;
}
}
并->add('answers', AnswersType::class)
在每种表单类型上使用,而不是在大块上使用。
由于某种原因,生成的标记并不相同:
没有AnswersType
,会form_row()
生成以下标记:
<fieldset class="form-group"><div id="campaign_trigger_answers" class="collection" data-prototype=" <div class="row answer-row" style="margin-bottom: 5px;" id="answer-nb-__name__"> <div class="col-10"> <input type="text" id="campaign_trigger_answers___name__" name="campaign[trigger][answers][__name__]" placeholder="Saisir une réponse" class="answer-input form-control" /> </div> <div class="col-2 text-right"> <a href="#" data-index="__name__" class="remove-answer btn btn-danger">X</a> </div> </div> "></div></fieldset>
使用AnswerType
,form_row()
生成以下标记:
<fieldset class="form-group"><div id="campaign_trigger_answers"></div></fieldset>
如您所见,prototype
AnswersType 不再生成任何属性,即使我已将该选项设置为 true。我的错误在哪里?
解决方案
推荐阅读
- javascript - 如果它不存在则打开窗口,如果它存在于Javascript中则最大化
- google-sheets - 一张图表中的三个 Google 表格的数据图表(饼图)
- iphone - 构建一个使用 React Native 创建的 iPhone 应用程序
- axon - 如何在 Saga 中测试具有内部状态的方法
- git - Git克隆和修改开源作为子目录并与master保持更新
- elasticsearch - 从 Elasticsearch 获取不同的字段
- javascript - Laravel 不导入/读取公共源文件 (css/js)
- c# - 如何使用 RegAsm 调用方法 C# Dll 方法?
- swift - Swift 4.2 UIDocumentBrowser View Controller Powerpoint PPTX 文件灰显
- database - symfony 对数据库表名有什么建议?