【问题标题】:Symfony Embedded Form ValidationSymfony 嵌入式表单验证
【发布时间】:2017-08-04 21:21:08
【问题描述】:

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

我想基于组执行验证,因此 EmailType 必须仅在 Assert\NotBlank(默认组)和 Assert\Email(电子邮件组)上检查“elemento”属性,而 Telefono 必须检查 Assert\NotBlank(默认组)和 Assert\Regex(电话组)。

在我的配置下,两个检查(约束)都被执行了,所以电子邮件是在电子邮件约束和正则表达式上检查的,所以电话归档是......我哪里错了?

收集电子邮件和电话上的员工实体已配置 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'])

    }

}

Email 和 Telefono 都扩展

<?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;

【问题讨论】:

    标签: php symfony validation symfony-forms symfony-validator


    【解决方案1】:

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

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

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

    【讨论】:

    • 非常感谢。 'validation_groups' 属性误导了我,我认为它可以用来“指导”验证。再次感谢您。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多