0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

CraueFormFlowBundleを使った時にバリデーションが効かない時の解決法

0
Last updated at Posted at 2021-09-30

Symfonyでマルチステップなフォームを作る時に便利なCraueFormFlowBundleというライブラリがあります。

こちらを使って、マルチステップなフォームを作った時にバリデーションが効かない時があったので、その時の対応方です。

事象

このようなフォームを作った時に、フォームタイプ全体にかかるバリデーションが効かず、field1field2の両方が空でもバリデーションが通ってしまった。

src/Form/TestFormType.php

class TestFormType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('field1', TextType::class, [
                'required' => false
            ])
            ->add('field2', TextType::class, [
                'required' => false
            ])
        ;
    }

    public function configureOptions(OptionsResolver $resolver)
    {
        $resolver->setDefaults([
            'data_class' => Data::class,
            'constraints' => [
                new Callback([
                    'callback' => function (Data $data, ExecutionContextInterface $context) {
                        if ($data->getField1() === null && $data->getField2() === null) {
                            $context->buildViolation('どちらか一つは入力してください。')
                                ->atPath('field1')
                                ->addViolation();
                            $context->buildViolation('どちらか一つは入力してください。')
                                ->atPath('field2')
                                ->addViolation();
                        }
                    },
                ]),
            ],
        ]);
    }
}
src/FormFlow/TestFormFlow.php
class TestFormFlow extends FormFlow
{
    protected function loadStepsConfig()
    {
        return [
            [
                'label' => 'step1',
                'form_type' => TestFormType::class,
            ],
            [
                'label' => 'confirm',
                'form_type' => FormType::class,
            ],
        ];
    }
}

解決策

CraueFormFlowBundleを使うと、フォームのバリデーショングループがDefaultから変わってしまうのが原因だった。
FromTypeで指定しているバリデーションは、バリデーショングループがDefaultとして設定されているので、バリデーショングループにDefaultが設定されていないと、バリデーションのチェックが行われないということでした。

FormFlowの方で、バリデーショングループをDefaultとするように設定してあげればOK。

src/FormFlow/TestFormFlow.php
class TestFormFlow extends FormFlow
{
    protected function loadStepsConfig()
    {
        return [
            [
                'label' => 'step1',
                'form_type' => TestFormType::class,
                'form_options' => [
                    'validation_groups' => ['Default'],
                ],
            ],
            [
                'label' => 'confirm',
                'form_type' => FormType::class,
            ],
        ];
    }
}
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?