Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support of recaptcha v3 #358

Open
wants to merge 2 commits into
base: 2.5
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions DependencyInjection/SuluFormExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@

use Sulu\Bundle\FormBundle\Controller\FormTokenController;
use Sulu\Bundle\FormBundle\Controller\FormWebsiteController;
use Sulu\Bundle\FormBundle\Dynamic\Types\RecaptchaType;
use Sulu\Bundle\FormBundle\Dynamic\Types\RecaptchaV3Type;
use Sulu\Bundle\FormBundle\Entity\Form;
use Sulu\Component\HttpKernel\SuluKernel;
use Symfony\Component\Config\FileLocator;
Expand Down Expand Up @@ -223,6 +225,11 @@ public function load(array $configs, ContainerBuilder $container): void
}

if (\class_exists(\EWZ\Bundle\RecaptchaBundle\Form\Type\EWZRecaptchaType::class)) {
if (3 == $container->getParameter('ewz_recaptcha.version')) {
$container->setParameter('recaptcha_type.class', RecaptchaV3Type::class);
} else {
$container->setParameter('recaptcha_type.class', RecaptchaType::class);
}
$loader->load('type_recaptcha.xml');
}

Expand Down
47 changes: 47 additions & 0 deletions Dynamic/Types/RecaptchaV3Type.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php

/*
* This file is part of Sulu.
*
* (c) Sulu GmbH
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/

namespace Sulu\Bundle\FormBundle\Dynamic\Types;

use Sulu\Bundle\FormBundle\Dynamic\FormFieldTypeConfiguration;
use Sulu\Bundle\FormBundle\Dynamic\FormFieldTypeInterface;
use Sulu\Bundle\FormBundle\Entity\FormField;
use Symfony\Component\Form\FormBuilderInterface;

/**
* The Recaptcha form field type.
*/
class RecaptchaV3Type implements FormFieldTypeInterface
{
use SimpleTypeTrait;

public function getConfiguration(): FormFieldTypeConfiguration
{
return new FormFieldTypeConfiguration(
'sulu_form.type.recaptcha',
__DIR__ . '/../../Resources/config/form-fields/field_recaptcha.xml',
'special'
);
}

public function build(FormBuilderInterface $builder, FormField $field, string $locale, array $options): void
{
// Use in this way the recaptcha bundle could maybe not exists.
$options['mapped'] = false;
$options['constraints'] = new \EWZ\Bundle\RecaptchaBundle\Validator\Constraints\IsTrueV3();
$options['attr']['options'] = [
'theme' => 'light',
'type' => 'image',
'size' => 'normal',
];
$builder->add($field->getKey(), \EWZ\Bundle\RecaptchaBundle\Form\Type\EWZRecaptchaV3Type::class, $options);
}
}
2 changes: 1 addition & 1 deletion Resources/config/type_recaptcha.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">
<services>
<service id="sulu_form.dynamic.type_recaptcha" class="Sulu\Bundle\FormBundle\Dynamic\Types\RecaptchaType">
<service id="sulu_form.dynamic.type_recaptcha" class="%recaptcha_type.class%">
<tag name="sulu_form.dynamic.type" alias="recaptcha"/>
</service>
</services>
Expand Down