我有一个带收藏的表格
class EventType extends AbstractType
{
/**
* {@inheritdoc}
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add('header', TextType::class, ['attr' => ['class' => 'form-control']])
->add('text', TextareaType::class, ['attr' => ['class' => 'form-control', 'rows' => '3']])
->add('fileNames', CollectionType::class, [
'mapped' => false,
'allow_add' => true,
'allow_delete' => true,
'by_reference' => false,
'label' => false,
'entry_type' => EventFileType::class,
])
->add('save', SubmitType::class, [
'label' => 'Post',
'attr' => [
'class' => 'btn btn-success',
'style' => 'margin-top: 10px;'
]
]);
}
/**
* {@inheritdoc}
*/
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults([
'data_class' => Event::class,
'csrf_token_id' => 'eventToken',
'allow_extra_fields' => true,
]);
}
}
这是要收集的条目类型
class EventFileType extends AbstractType
{
/**
* {@inheritdoc}
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add('fileName', FileType::class);
}
/**
* {@inheritdoc}
*/
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults(array(
'method' => 'POST',
'csrf_token_id' => 'eventFiles',
));
}
}
我的表格如下:
{{ form_start(form, {'attr': {'id': 'form_create_event'}}) }}
<ul class="tags" data-prototype="{{ form_widget(form.fileNames.vars.prototype)|e('html_attr') }}">
</ul>
{{ form_end(form) }}
使用js添加字段。和ajax:
.ajax({
type: "POST",
url: '{{ path('event_create') }}',
contentType: false,
cache: false,
processData: false,
data: new FormData(this),
所以我得到文本输入,并且$ _FILE不为空。 $form['fileNames']->getData()
将显示文件,但字段为空,因此,如果我输入2个文件,将是:[{"fileName":{}},{"fileName":{}}]
,一个文件-[{"fileName":{}}]
,超过3个文件说,并且格式无效。那么我怎样才能使我的表格获取这些文件呢? (我没有映射它们),如何获得所需的文件数量?