craue / CraueFormFlowBundle

Multi-step forms for your Symfony project.
MIT License
736 stars 118 forks source link

Using Captcha at the last step of a form #315

Closed pculka closed 6 years ago

pculka commented 6 years ago

Hi, I've created a simple 2-step form using two form types. The form type has included:

$builder->add('recaptcha', EWZRecaptchaType::class, [
                'validation_groups' => ['manual', 'anonymous'],
                **'mapped' => false,**
                'attr' => [
                    'options' => [
                        'theme' => 'light',
                        'type' => 'image',
                        'size' => 'normal',
                        'defer' => true,
                        'async' => true,
                    ],
                ],
            ]);

But the validation never triggers, even though it's in the flow definition of steps:

protected function loadStepsConfig(): array
    {
        return [
            [
                'label' => 'Add',
                'form_type' => UNPSubmissionType::class,
                'form_options' => [
                    'anonymous_submission' => true,
                    'validation_groups' => ['anonymous']
                ]

            ],
            [
                'label' => 'Submit',
                'form_type' => UNPSubmissionTypeAnonymous::class,
                'form_options' => [
                    'anonymous_submission' => true,
                    'validation_groups' => ['anonymous']
                ]
            ],
        ];
    }

I don't get it why it doesn't trigger? When I add the field to the mapped entity, it just works but that's wrong.

craue commented 6 years ago

You need to add some validation constraint directly to the unmapped field. Like this (untested):

use EWZ\Bundle\RecaptchaBundle\Validator\Constraints\IsTrue as RecaptchaTrue;

$builder->add('recaptcha', EWZRecaptchaType::class, [
    'mapped' => false,
    'constraints' => [
        new RecaptchaTrue([
            'validation_groups' => ['manual', 'anonymous'],
        ])
    ],
    'attr' => [
        'options' => [
            'theme' => 'light',
            'type' => 'image',
            'size' => 'normal',
            'defer' => true,
            'async' => true,
        ],
    ],
]);
pculka commented 6 years ago

nopes, it simply does not trigger

pculka commented 6 years ago

When I added a recaptcha field to the Entity, it started validating, but it always triggered an error. Dis weird :/

pculka commented 6 years ago

oh darn! It worked, the private key was wrong :/