diff --git a/Dynamic/Types/RecaptchaType.php b/Dynamic/Types/RecaptchaType.php index fd5af178..d529c250 100644 --- a/Dynamic/Types/RecaptchaType.php +++ b/Dynamic/Types/RecaptchaType.php @@ -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; /** @@ -23,6 +24,19 @@ class RecaptchaType implements FormFieldTypeInterface { use SimpleTypeTrait; + /** + * @var ParameterBagInterface + */ + private $params; + + /** + * @param ParameterBagInterface $params + */ + public function __construct($params) + { + $this->params = $params; + } + public function getConfiguration(): FormFieldTypeConfiguration { return new FormFieldTypeConfiguration( @@ -35,13 +49,25 @@ public function getConfiguration(): FormFieldTypeConfiguration 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') + && 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']); + } + + $options['constraints'] = $constraint; + + $builder->add($field->getKey(), $type, $options); } } diff --git a/Resources/config/type_recaptcha.xml b/Resources/config/type_recaptcha.xml index 09112939..d452aca3 100644 --- a/Resources/config/type_recaptcha.xml +++ b/Resources/config/type_recaptcha.xml @@ -5,6 +5,7 @@ xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd"> + diff --git a/Resources/doc/recaptcha.md b/Resources/doc/recaptcha.md index 0994b667..5fdb3ed0 100644 --- a/Resources/doc/recaptcha.md +++ b/Resources/doc/recaptcha.md @@ -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: private_key: