Skip to content

Commit

Permalink
Fix: threshold typo + add error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
tvlooy committed Dec 14, 2023
1 parent 371f67d commit d8de1a2
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 8 deletions.
19 changes: 14 additions & 5 deletions src/Form/Handler/ContactFormHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,15 +40,24 @@ public function handle(FormInterface $form, Request $request): bool

/** @var ContactSubmission $data */
$data = $form->getData();

$result = $this->recaptcha->validateRecaptchaToken($data->getRecaptchaToken());
$captchaResult = $this->recaptcha->validateRecaptchaToken($data->getRecaptchaToken());

// Client succeed recaptcha validation.
if ($result['success'] && $this->mailer->sendContactFormEmail($data)) {
$this->translator->setLocale($request->getLocale());
$this->session->getFlashBag()->add('success', $this->translator->trans('flashes.contact.success'));
if ($captchaResult['success'] !== true) {
$this->session->getFlashBag()->add('danger', 'You seem like a robot, sorry.');
return false;
}

$mailResult = $this->mailer->sendContactFormEmail($data);

if (!$mailResult) {
$this->session->getFlashBag()->add('danger', 'Mail was not sent due to unknown error.');
return false;
}

$this->translator->setLocale($request->getLocale());
$this->session->getFlashBag()->add('success', $this->translator->trans('flashes.contact.success'));

return true;
}
}
2 changes: 1 addition & 1 deletion src/Service/RecaptchaService.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public function validateRecaptchaToken(string $token): array
$resp = $recaptcha
->setExpectedHostname($_SERVER['SERVER_NAME'])
->setExpectedAction($this->recaptchaSecret->action)
->setScoreThreshold($this->recaptchaSecret->treshold)
->setScoreThreshold($this->recaptchaSecret->threshold)
->verify($token, $_SERVER['REMOTE_ADDR']);

return $resp->toArray();
Expand Down
3 changes: 1 addition & 2 deletions templates/base.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,6 @@
style="display:none;visibility:hidden"></iframe>
</noscript>

{% block headScripts %} {% endblock headScripts %}

<script>
(function (w, d, s, l, i) {
w[l] = w[l] || [];
Expand All @@ -67,6 +65,7 @@
</script>
<!-- End Google Tag Manager -->
{% endif %}
{% block headScripts %} {% endblock headScripts %}
<div id="header" class="header">
<div class="container">
<div class="header__left" onclick="window.location='{{ url('homepage') }}'">
Expand Down

0 comments on commit d8de1a2

Please sign in to comment.