1

我有几个实体,它们以单一形式使用,从抽象类扩展而来。我为每个实体创建了一个表单类型,然后将它们嵌入到父表单中。

我想基于组执行验证,因此 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;
4

1 回答 1

1

AFAIK 你不能validation_groupsCollectionType表单字段上设置(你可以设置它,但它没有效果),所以整个表单,包括集合中的子表单集总是validation_groups在整个父表单上使用 set 进行验证。

的目的validation_groups是允许针对不同目的(例如创建新对象与编辑现有对象)对对象属性进行约束修改,但不适用于您上面描述的内容。 考虑一下,如果可能的话,直接在 Staff 对象(或分别为 StaffType)中使用当前的 Email 和 Telephono 作为属性,并使用 validation_groups 来解决 $elemento 应该是 Email elemento once 和 Telephono elemento once ...

您的案例的解决方案是将 Email 和 Telephono 定义为不同的类(不是从 AbstractElementoContact 继承的),每个类都有特定的约束。

于 2017-03-15T22:54:41.890 回答