我有一个这样的表单:
class AssignmentType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('ref')
->add('rate')
->add('costs')
->add('notice', TextareaType::class, [
'required' => false,
'attr' => ['class' => 'summernote'],
])
->add('documents', CollectionType::class, [
'entry_type' => DocumentType::class,
'entry_options' => ['label' => false],
'allow_add' => true,
'allow_delete' => true,
'by_reference' => false,
'required' => false,
'label' => false,
])
;
}
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults([
'data_class' => Assignment::class,
]);
}
}
和另一个表单类型,像这样:
class DocumentType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('file', FileType::class, [
'label' => false,
'required' => true,
'constraints' => [
new File(),
],
]);
}
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults([
'data_class' => Document::class,
]);
}
}
所以一个作业可以有多个文档。DOM是这样的:
<div class="custom-file">
<input type="file" id="assignment_documents_0_file" name="assignment[documents][0][file]" class="custom-file-input">
<label for="assignment_documents_0_file" lang="de" class="custom-file-label"></label>
</div>
但是表单包含错误-$form->isValid()
返回false。错误是:
这个表单不应该包含额外的字段。
实体赋值文档有以下条目:
/**
* @ORMOneToMany(targetEntity=Document::class, mappedBy="assignment", cascade={"persist"}, orphanRemoval=true)
* @Expose
*/
private Collection $documents;
怎么了?
提示:从PHP 7.4升级到PHP 8.1或PHP 8.2似乎有问题但我看不出有什么问题。在我将PHP降级到7.4之后,它可以工作- form是有效的。
Symfony 5.4已经解决了这个问题。它适用于Symfony 5.1和PHP 7.4或Symfony 5.4和PHP 8.1。或8.2
所以它与Symfony和PHP版本不兼容。也许它也可以工作在PHP 8.1/8.2和Symfony 5.2