我创建了2个实体,其中一个实体用作另一个实体的集合。
在两个实体上我想应用验证规则,但是当我这样做时,我对集合的验证有两个问题:
如果我填写所有必填字段(在main和集合中),则实体将被保留,
但是如果我没有填写一个必填字段,
我永远不会坚持,因为即使我填写了所有强制性的 字段我仍然有集合的验证错误消息
当我提交集合中填写的(其他)数据丢失时
奇怪(或不是)是如果我填写主表格的强制性部分而不是集合的强制部分,如果我提交,则保留主要部分的数据。但反过来却没有。
我真的不明白为什么会发生这种情况,并且在其他帖子中没有看到相同行为的描述。
带注释的实体(相关部分):
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
use Symfony\Component\Validator\Constraints as Assert;
/**
* @ORM\Table(name="institution")
* @ORM\Entity(repositoryClass="MyBundle\Repository\InstitutionRepository")
* @UniqueEntity(fields={"name"},errorPath="name",message="An institution with the same name exists.")
*/
class Institution
{
/**
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="IDENTITY")
*/
private $id;
/**
* @ORM\Column(name="name", type="string", length=255, nullable=false)
* @Assert\NotBlank(message="Vous devez entrer un nom pour l'institution")
*/
private $name;
/**
* @ORM\OneToMany(targetEntity="MyBundle\Entity\InstitutionContact", mappedBy="institution", cascade={"persist", "remove"}, orphanRemoval=TRUE)
* @Assert\Valid
*/
protected $institutionContacts;
}
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
use Symfony\Component\Validator\Constraints as Assert;
/**
* InstitutionContact.
*
* @ORM\Table(name="institutioncontact", indexes={
* @ORM\Index(name="FK_InstitutionContact_institutionId", columns={"institutionId"}),
* @ORM\Index(name="FK_InstitutionContact_userId", columns={"userId"}),
* })
* @ORM\Entity(repositoryClass="Simusante\SimustoryBundle\Repository\InstitutionContactRepository")
* @UniqueEntity(fields={"institution", "contact"},errorPath="departmentName",message="This contact and this institution are already connected."
* )
*/
class InstitutionContact
{
/**
* @ORM\ManyToOne(targetEntity="MyBundle\Entity\Institution", inversedBy="institutionContacts")
* @ORM\JoinColumn(name="institutionId", referencedColumnName="id", nullable=false)
* @Assert\NotBlank(message="You have to choose an institution to link it to a contact")
*/
private $institution;
/**
* @ORM\ManyToOne(targetEntity="MyBundle\Entity\User", inversedBy="institutionContacts")
* @ORM\JoinColumn(name="userId", referencedColumnName="id", nullable=false)
* @Assert\NotBlank(message="ou have to choose a contact to link it to an institution")
*/
private $contact;
}
在我的formbuilder中:
class InstitutionType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('name', TextType::class, [
'label' => 'name',
'label_attr' => ['class' => 'mandatory'],
])
->add('institutionContacts', CollectionType::class, [
'label' => 'user.institutionContact',
'attr' => [
'class' => 'collectiondata',
],
'required' => true,
'constraints' => new Valid(),
'entry_type' => new InstitutionContactType($this->manager),
'by_reference' => false,
'allow_add' => true,
'allow_delete' => true,
]);
...
}
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults([
'data_class' => Institution::class,
'translation_domain' => 'SimusanteSimustoryBundle',
'attr'=>['novalidate'=>'novalidate'], //disable html5 validation
]);
}
}