我有几个实体,它们以单一形式使用,从抽象类扩展而来。我为每个实体创建了一个表单类型,然后将它们嵌入到父表单中。
我想基于组执行验证,因此 EmailType 必须仅在 Assert\NotBlank(默认组)和 Assert\Email(电子邮件组)上检查“elemento”属性,而 Telefono 必须检查 Assert\NotBlank(默认组)和断言\正则表达式(电话组)。
使用我的配置执行两项检查(约束),因此在电子邮件约束和正则表达式上检查电子邮件,因此提交的电话是......我哪里错了?
收集电子邮件和电话上的员工实体已配置 Assert\Valid() 约束
这是示例
父表格
<?php
namespace App\Form\Staff;
class StaffType extends AbstractType {
public function configureOptions(OptionsResolver $resolver) {
$resolver->setDefaults(['data_class' => \Cowbell\Entity\Staff\Staff::class,
'validation_groups' => ['Default', 'email', 'phone']]);
}
/**
*
* @param FormBuilderInterface $builder
* @param array $options
*/
public function buildForm(FormBuilderInterface $builder, array $options) {
// ... Other field of Staff Entity
->add('emails', CollectionType::class, ['label' => 'admin.emails',
'entry_type' => \App\Form\Contatto\CBEmailType::class,
'entry_options' => ['label' => false],
'allow_add' => true,
'allow_delete' => true,
'empty_data' => null,
'translation_domain' => 'admin',
'validation_groups' => ['email']])
->add('telefoni', CollectionType::class, ['label' => 'admin.phones',
'entry_type' => \App\Form\Contatto\CBTelefonoType::class,
'entry_options' => ['label' => false],
'allow_add' => true,
'allow_delete' => true,
'empty_data' => null,
'translation_domain' => 'admin',
'validation_groups' => ['phone']]);
}
}
然后是 CBEmailType
<?php
namespace App\Form\Contatto;
class CBEmailType extends AbstractType{
/**
*
* @param OptionsResolver $resolver
*/
public function configureOptions( OptionsResolver $resolver)
{
$resolver->setDefaults(['data_class' => \App\Entity\Contatto\Email::class,
'validation_groups' => ['Default', 'email']]);;
}
public function buildForm(FormBuilderInterface $builder, array $options) {
$builder->add('elemento', EmailType::class, ['label' => 'admin.email',
'translation_domain' => 'admin'])
}
}
CBTelefonoType
<?php
namespace App\Form\Contatto;
class CBTelefonoType extends AbstractType{
/**
*
* @param OptionsResolver $resolver
*/
public function configureOptions( OptionsResolver $resolver)
{
$resolver->setDefaults(['data_class' => \Cowbell\Entity\Contatto\Telefono::class,
'validation_groups' => ['Default', 'phone']]);
}
public function buildForm(FormBuilderInterface $builder, array $options) {
$builder->add('elemento', TextType::class, ['label' => 'admin.phone',
'translation_domain' => 'admin'])
}
}
电子邮件和电话都扩展
<?php
namespace App\Entity\Contact;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;
abstract class AbstractElementoContact {
/**
*
* @var string
*
* @ORM\Column(name="elemento", type="string", length=100, nullable=false)
* @Assert\NotBlank()
* @Assert\Email(strict=true, checkHost=true, checkMX=true, groups={"email"})
* @Assert\Regex("/[0-9]{6,50}/", groups={"phone"})
*/
protected $elemento;