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

DRAFT: Extend with recaptcha v3 #373

Closed
wants to merge 14 commits into from
30 changes: 28 additions & 2 deletions Dynamic/Types/RecaptchaType.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use Sulu\Bundle\FormBundle\Dynamic\FormFieldTypeConfiguration;
use Sulu\Bundle\FormBundle\Dynamic\FormFieldTypeInterface;
use Sulu\Bundle\FormBundle\Entity\FormField;
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
use Symfony\Component\Form\FormBuilderInterface;

/**
Expand All @@ -23,6 +24,19 @@
{
use SimpleTypeTrait;

/**
* @var ParameterBagInterface
*/
private $params;

/**
* @param ParameterBagInterface $params
*/
public function __construct($params)
{
$this->params = $params;
}

public function getConfiguration(): FormFieldTypeConfiguration
{
return new FormFieldTypeConfiguration(
Expand All @@ -35,13 +49,25 @@
public function build(FormBuilderInterface $builder, FormField $field, string $locale, array $options): void
{
// Use in this way the recaptcha bundle could maybe not exists.
$constraint = new \EWZ\Bundle\RecaptchaBundle\Validator\Constraints\IsTrue();
$type = \EWZ\Bundle\RecaptchaBundle\Form\Type\EWZRecaptchaType::class;
$options['mapped'] = false;
$options['constraints'] = new \EWZ\Bundle\RecaptchaBundle\Validator\Constraints\IsTrue();
$options['attr']['options'] = [
'theme' => 'light',
'type' => 'image',
'size' => 'normal',
];
$builder->add($field->getKey(), \EWZ\Bundle\RecaptchaBundle\Form\Type\EWZRecaptchaType::class, $options);

if ($this->params->has('ewz_recaptcha.version')

Check failure on line 61 in Dynamic/Types/RecaptchaType.php

View workflow job for this annotation

GitHub Actions / PHP Lint

Left side of && is always false.
&& 3 == $this->params->get('ewz_recaptcha.version')
) {
$constraint = new \EWZ\Bundle\RecaptchaBundle\Validator\Constraints\IsTrueV3();
$type = \EWZ\Bundle\RecaptchaBundle\Form\Type\EWZRecaptchaV3Type::class;
unset($options['attr']);
}
Comment on lines +61 to +67
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Quehnie This should be done in a compilerpass. There you have access to the ewz_recpatcha.version even the bundle is registered after the form bundle:

And then you can set a sulu_form.recaptcha_version parameter which you use in type_reacptacha.xml


$options['constraints'] = $constraint;

$builder->add($field->getKey(), $type, $options);
}
}
1 change: 1 addition & 0 deletions Resources/config/type_recaptcha.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
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">
<argument id="parameter_bag" type="service"/>
<tag name="sulu_form.dynamic.type" alias="recaptcha"/>
</service>
</services>
Expand Down
2 changes: 1 addition & 1 deletion Resources/doc/recaptcha.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ EWZ\Bundle\RecaptchaBundle\EWZRecaptchaBundle::class ['all' => true],

add the following config to `config/packages/ewz_recaptcha.yaml`

```yml
```yaml
ewz_recaptcha:
public_key: <insert_your_public_key>
private_key: <insert_your_private_key>
Expand Down
Loading