现在我已经成功地使用了验证组,但现在我被验证组和嵌套映射实体困住了。
我将通过一个简化的例子来解释这个问题。
我的实体:地址、损坏、电器
/**
* @ORM\Entity()
*/
class Address extends ...
{
/**
* @var string
* @ORM\Column(type="string", name="postcode", nullable=true)
* @Assert\NotBlank(
* groups={
* "damage_responsible_address",
* "appliance_repairer_address",
* })
*/
private $postcode;
...
/**
* @ORM\Entity()
*/
class Damage extends ...
{
/**
* @var boolean
* @ORM\Column(type="boolean", name="responsible", nullable=true)
* @Assert\NotBlank(groups={"damage"})
*/
private $responsible;
/**
* @ORM\OneToOne(targetEntity="Address", cascade={"persist","remove"})
* @ORM\JoinColumn(name="responsible_address_id", referencedColumnName="id")
* @Assert\Valid()
*/
private $responsibleAddress;
/**
* @ORM\ManyToMany(targetEntity="Appliance", orphanRemoval=true, cascade={"persist", "remove"})
* @ORM\JoinTable(name="coronadirect_cuzo_home_damage_appliances",
* joinColumns={@ORM\JoinColumn(name="damage_id", referencedColumnName="id")},
* inverseJoinColumns={@ORM\JoinColumn(name="appliance_id", referencedColumnName="id")}
* )
*/
private $appliances;
...
/**
* @ORM\Entity()
*/
class Appliance extends ...
{
/**
* @var boolean
* @ORM\Column(type="boolean", name="to_repair", nullable=true)
* @Assert\NotBlank(groups={"appliance"})
*/
private $toRepair;
/**
* @ORM\OneToOne(targetEntity="Address", cascade={"persist","remove"})
* @ORM\JoinColumn(name="repairer_address_id", referencedColumnName="id")
* @Assert\Valid()
*/
private $repairAddress;
...
为了定义我的表单,我使用了 AddressType、DamageType 和 ApplianceType:
class DamageType extends ...
{
/**
* {@inheritdoc}
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add('appliances', 'collection', array(
'type' => 'home_damage_appliance_type',
'allow_add' => true,
'allow_delete' => true,
'prototype' => true,
'options' => array(
'cascade_validation' => true,
)
));
$builder->add('responsible', 'choice', array(
'choices' => $this->getYesNoChoiceArray(),
'expanded' => true,
'multiple' => false,
));
$builder->add('responsibleAddress', 'address_type', array(
'required' => true
));
...
}
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$resolver->setDefaults(array(
'data_class' => 'Damage',
'cascade_validation' => true,
'validation_groups' =>
function(FormInterface $form) {
$groups = array('damage');
if ($form->getData()->getResponsible() == true) {
$groups[] = 'damage_responsible_address';
}
return $groups;
}
));
}
当在表单中将责任设置为 true 时,我将添加damage_responsible_address 组。否则我不希望验证地址。
class ApplianceType extends ...
{
/**
* {@inheritdoc}
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add('toRepair', 'choice', array(
'choices' => $this->getYesNoChoiceArray(),
'expanded' => true,
'multiple' => false,
));
$builder->add('repairAddress', 'address_type', array(
'required' => true
));
...
}
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$resolver->setDefaults(array(
'data_class' => 'Appliannce',
'cascade_validation' => true,
'validation_groups' =>
function(FormInterface $form) {
$groups = array('appliance');
if ($form->getData()->getToRepair() == true) {
$groups[] = 'appliance_repairer_address';
}
return $groups;
}
));
}
和之前一样,当 toRepair 为真时,我想验证地址。
怎么了?
我负责的损坏是真的,而要修复的设备是假的,表格确实在负责的地址上给出了验证错误,但也在设备地址上给出了验证错误。
反之亦然:当设备地址无效时(toRepar 为真),那么责任地址也无效(即使责任为假)。
地址验证组不查看它们定义的表单,而只是将它们附加到表单中的每个地址项。
是否可以仅为表单定义特定的验证组?
我正在使用 Doctrine 和 Symfony 2.3.6。