当使用javascript在DOM中动态添加/删除symfony收集字段时,除了此集合之外,验证或持久收集元素都会搞砸。例如,如果我有Entity \ User.php版本,如:
/**
* @var \TestBundle\Entity\UserFollowers
*
* @ORM\OneToMany(targetEntity="TestBundle\Entity\UserFollowers", mappedBy="user", cascade={"persist"}, orphanRemoval=true)
* @Assert\Valid()
*/
protected $user_followers;
和向后的Entity \ UserFollower.php关系如:
/**
* @ORM\ManyToOne(targetEntity="TestBundle\Entity\User", inversedBy="user_followers")
* @ORM\JoinColumn(name="user_id", referencedColumnName="id")
*/
protected $user;
/**
* @var string
* @ORM\Column(name="name", type="string")
* @Assert\NotBlank()
*/
private $name;
和UserFormType一样:
$builder->add('user_followers', CollectionType::class, array(
'entry_type' => UserFollowersType::class,
'allow_add' => true,
'allow_delete' => true,
'prototype' => true,
'prototype_name' => '__name__',
'by_reference' => false,
'label' => 'User followers'
))
和UserFollowersFormType类似:
public function buildForm(FormBuilderInterface $builder, array $options) {
$builder
->add('name', TextType::class, array(
'attr'=> [
'class' =>'input input-group-field'
],
'label' => false
));
}
当表单被处理并且用户持久化到db时,表单验证附加到错误的集合elem或持久化id到db出错,不同的DOM操作得到不同的错误,控制器就像:
$form->handleRequest($request);
if ($form->isValid()) {
$em->persist($user);
$em->flush();
}
为什么会这样?它的形式集合索引搞砸了......
答案 0 :(得分:0)
动态添加新的UserFollowersType表单时,还必须将byte[] buffer = new byte[4096];
int n = 0;
while (-1 != (n = is.read(buffer))) {
fos.write(buffer, 0, n);
}
prototype_name
替换为集合的长度。
关于集合init:
__name__