我想在表单构建器中添加许多隐藏文件,其名称类似于数组。例如:
<input type="hidden" name="test[]">
<input type="hidden" name="test[]">
<input type="hidden" name="test[]">
<input type="hidden" name="test[]">
我怎么能这样做?感谢。
$builder->addEventListener(FormEvents::PRE_SET_DATA, function(FormEvent $event) {
$form = $event->getForm();
$photos = $event->getData()->getPhotos();
if ($photos) {
foreach ($photos as $photo) {
$form->add('uploadedPhoto', CollectionType::class, array(
'entry_type' => HiddenType::class,
// 'data' => $photo->getId(),
'mapped' => false,
));
}
}
});
答案 0 :(得分:2)
您需要使用collection
类型:
use Symfony\Component\Form\Extension\Core\Type\CollectionType;
use Symfony\Component\Form\Extension\Core\Type\HiddenType;
$builder->add('foo', CollectionType::class , array('entry_type' => HiddenType::class));
我假设您要在数据源的photos
属性下列出隐藏的所有元素。
$builder->add('photos', CollectionType::class , array(
'entry_type' => HiddenType::class,
'mapped' => false
));
详细了解此字段类型:http://symfony.com/doc/current/reference/forms/types/collection.html#entry-type