Skip to content

Commit

Permalink
feat: allow you to disable recaptcha
Browse files Browse the repository at this point in the history
  • Loading branch information
delyriand committed Apr 2, 2024
1 parent ffd3897 commit d93d02a
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 11 deletions.
19 changes: 15 additions & 4 deletions src/Form/Extension/ContactFormExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,28 @@

final class ContactFormExtension extends AbstractTypeExtension
{
private bool $karserRecaptcha3Enabled;

public function __construct(bool $karserRecaptcha3Enabled)
{
$this->karserRecaptcha3Enabled = $karserRecaptcha3Enabled;
}

/**
* @inheritDoc
*/
public function buildForm(FormBuilderInterface $builder, array $options): void
{
$constraints = [
new Recaptcha3Constraint(),
];
if ($this->karserRecaptcha3Enabled) {
$constraints[] = new Assert\NotBlank();
}

$builder->add('captcha', Recaptcha3Type::class, [
'mapped' => false,
'constraints' => [
new Recaptcha3Constraint(),
new Assert\NotBlank(),
],
'constraints' => $constraints,
'action_name' => 'contact',
]);
}
Expand Down
19 changes: 15 additions & 4 deletions src/Form/Extension/CustomerRegistrationFormExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,28 @@

final class CustomerRegistrationFormExtension extends AbstractTypeExtension
{
private bool $karserRecaptcha3Enabled;

public function __construct(bool $karserRecaptcha3Enabled)
{
$this->karserRecaptcha3Enabled = $karserRecaptcha3Enabled;
}

/**
* @inheritDoc
*/
public function buildForm(FormBuilderInterface $builder, array $options): void
{
$constraints = [
new Recaptcha3Constraint(['groups' => 'sylius_user_registration']),
];
if ($this->karserRecaptcha3Enabled) {
$constraints[] = new Assert\NotBlank(['groups' => 'sylius_user_registration']);
}

$builder->add('captcha', Recaptcha3Type::class, [
'mapped' => false,
'constraints' => [
new Recaptcha3Constraint(['groups' => 'sylius_user_registration']),
new Assert\NotBlank(['groups' => 'sylius_user_registration']),
],
'constraints' => $constraints,
'action_name' => 'register',
]);
}
Expand Down
8 changes: 5 additions & 3 deletions src/Resources/views/Shop/form_captcha.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
{% if errors is not empty %}
{{ errors|raw }}<br />
{% endif %}
<div class="ui info message">
{{ 'monsieurbiz_anti_spam.form_captcha.message'|trans({'%privacy_link%': 'https://policies.google.com/privacy', '%terms_link%': 'https://policies.google.com/terms'})|raw }}
</div>
{% if form.captcha.vars.enabled -%}
<div class="ui info message">
{{ 'monsieurbiz_anti_spam.form_captcha.message'|trans({'%privacy_link%': 'https://policies.google.com/privacy', '%terms_link%': 'https://policies.google.com/terms'})|raw }}
</div>
{%- endif %}

0 comments on commit d93d02a

Please sign in to comment.