Symfony3忽略了表单子项的validation.yml

时间:2016-06-21 00:54:26

标签: validation symfony

我不知道发生了什么,因为它曾经工作过,现在突然之间没有。

让我们从Controller开始:

        $user = new User();
        $studio = new Studio();
        $transformer = new StudioAccountDataTransformer($user, $studio);
        $form = $this->createForm(StudioAccountForm::class, $transformer);
        $form->handleRequest($request);

        if($form->isSubmitted() && $form->isValid())
        {
             // isValid always true
        }

现在是StudioAccountForm

public function buildForm(FormBuilderInterface $builder, array $option)
{
    $builder
        ->add('user', UserType::class, [
            'label' => false
        ])
        ->add('studio', StudioType::class, [
            'label' => false
        ])
    ;
}

public function configureOptions(OptionsResolver $resolver)
{
    $resolver->setDefaults([
        'translation_domain'    => 'general',
        'data_class'            => 'AppBundle\Entity\DataTransformer\StudioAccountDataTransformer',
        'attr'                  => ['novalidate' => 'novalidate'],
        'cascade_validation'    => true,
        'validation_groups'     => ['Default', 'admin']
    ]);
}

UserType(StudioType看起来很相似,没什么特别的)

use FirstNameField;
use LastNameField;
use EmailAddressField;
use SexField;

public function buildForm(FormBuilderInterface $builder, array $option)
{
    $this->firstName($builder);
    $this->lastName($builder);
    $this->emailAddress($builder);
    $this->sex($builder);
}

public function configureOptions(OptionsResolver $resolver)
{
    $resolver->setDefaults([
        'translation_domain'    => 'general',
        'data_class'            => 'AppBundle\Entity\User',
        'attr'                  => ['novalidate' => 'novalidate'],
        'csrf_protection'       => false,
        'studio'                => null,
        'expanded'              => false
    ]);
}

StudioAccountDataTransformer只是studiouser的getter / setter。 所以这是validation.yml

AppBundle\Entity\User:
    properties:
        firstName:
            - NotBlank:
                groups: [api, admin]
            - Length:
                min: 2
                max: 255
                groups: [api, admin]
        lastName:
            - NotBlank:
                groups: [api, admin]
            - Length:
                min: 2
                max: 255
                groups: [api, admin]  

并在config.yml我有

validation:      { enabled: true, enable_annotations: false }

正如您所看到的,StudioAccountForm还包含其他两种表单UserTypeStudioType,我应该从UserType收到错误,因为validation_groups配置正确。此外,还启用了cascade_validation。不知怎的,验证不起作用(我很确定它有效)。

注意:如果我只在UserType的createForm()函数中放置Controller,那么valiation就可以了。显然加载了.yml文件。

有什么想法吗?

1 个答案:

答案 0 :(得分:2)

最后我明白了。

所以从Symfony 2.8 log

The "cascade_validation" option was deprecated. Use the "constraints"
   option together with the `Valid` constraint instead. Contrary to
   "cascade_validation", "constraints" must be set on the respective child forms,
   not the parent form.

因此,我应该使用cascade_validation约束,而不是使用Valid,如下例所示

public function buildForm(FormBuilderInterface $builder, array $option)
{
    $builder
        ->add('user', UserType::class, [
            'label' => false,
            'constraints' => [
                new Valid()
            ]
        ])
        ->add('studio', StudioType::class, [
            'label' => false,
            'constraints' => [
                new Valid()
            ]
        ])
    ;
}

public function configureOptions(OptionsResolver $resolver)
{
    $resolver->setDefaults([
        'translation_domain'    => 'general',
        'data_class'            => 'AppBundle\Entity\DataTransformer\StudioAccountDataTransformer',
        'attr'                  => ['novalidate' => 'novalidate'],
        'validation_groups'     => ['Default', 'admin']
    ]);
}