Skip to content

Commit

Permalink
TASK: Add Turnstile reset link to validation errors in lazyload mode
Browse files Browse the repository at this point in the history
  • Loading branch information
jobee committed Feb 28, 2023
1 parent e1c3fba commit 580f941
Show file tree
Hide file tree
Showing 9 changed files with 38 additions and 16 deletions.
2 changes: 1 addition & 1 deletion Classes/Validation/Validator/TurnstileValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ class TurnstileValidator extends AbstractValidator
protected function isValid($value)
{
if (!is_string($value) || empty($value)) {
$this->addError('We could not identify you as a human. Please try again.', 1676890456);
$this->addError('The captcha challenge failed.', 1676890456);
return;
}

Expand Down
15 changes: 10 additions & 5 deletions Resources/Private/Form/Turnstile.html
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,16 @@
data-response-field-name="--{element.parentRenderable.parentRenderable.identifier}[{element.identifier}]"
></div>
<f:if condition="{validationResults.flattenedErrors}">
<span class="help-inline">
<f:for each="{validationResults.errors}" as="error">
<div>{error -> f:translate(id: '{error.code}', arguments: error.arguments, package: '{element.renderingOptions.validationErrorTranslationPackage}', source: 'ValidationErrors')}</div>
</f:for>
</span>
<div class="cf-turnstile-error">
<span class="help-inline">
<f:for each="{validationResults.errors}" as="error">
<div>{error -> f:translate(id: '{error.code}', arguments: error.arguments, package: '{element.renderingOptions.validationErrorTranslationPackage}', source: 'ValidationErrors')}</div>
</f:for>
</span>
<f:if condition="{element.properties.lazyload}">
<a data-cf-turnstile-reset href="javascript:;" onclick="javascript:cfTurnstileReset(this);">{f:translate(id: 'widget.reset.btn', package: 'Tms.Cloudflare.Turnstile')}</a>
</f:if>
</div>
</f:if>
</div>
</div>
Expand Down
4 changes: 4 additions & 0 deletions Resources/Private/Translations/de/Main.xlf
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@
<source>This form is protected by Cloudflare Turnstile. Please enable JavaScript for this page to identify you as a human.</source>
<target state="final">Dieses Formular ist durch Cloudflare Turnstile geschützt. Bitte aktivieren Sie JavaScript, damit diese Seite Sie als Mensch erkennen kann.</target>
</trans-unit>
<trans-unit id="widget.reset.btn" xml:space="preserve" approved="yes">
<source>Please click here to confirm that you are not a robot.</source>
<target state="final">Bitte klicken Sie hier, um zu bestätigen, dass Sie kein Roboter sind.</target>
</trans-unit>
</body>
</file>
</xliff>
8 changes: 4 additions & 4 deletions Resources/Private/Translations/de/ValidationErrors.xlf
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@

<!-- Override NotEmptyValidator translations for the Turnstile element -->
<trans-unit id="1221560910" xml:space="preserve" approved="yes">
<source>We could not identify you as a human. Please try again.</source>
<target state="final">Wir konnten Sie nicht als Mensch identifizieren. Bitte versuchen Sie es erneut.</target>
<source>The captcha challenge failed.</source>
<target state="final">Die Captcha-Abfrage ist fehlgeschlagen.</target>
</trans-unit>
<trans-unit id="1221560718" xml:space="preserve" approved="yes">
<source>We could not identify you as a human. Please try again.</source>
<target state="final">Wir konnten Sie nicht als Mensch identifizieren. Bitte versuchen Sie es erneut.</target>
<source>The captcha challenge failed.</source>
<target state="final">Die Captcha-Abfrage ist fehlgeschlagen.</target>
</trans-unit>
</body>
</file>
Expand Down
3 changes: 3 additions & 0 deletions Resources/Private/Translations/en/Main.xlf
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
<trans-unit id="widget.noScript.message">
<source>This form is protected by Cloudflare Turnstile. Please enable JavaScript for this page to identify you as a human.</source>
</trans-unit>
<trans-unit id="widget.reset.btn">
<source>Please click here to confirm that you are not a robot.</source>
</trans-unit>
</body>
</file>
</xliff>
4 changes: 2 additions & 2 deletions Resources/Private/Translations/en/ValidationErrors.xlf
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@

<!-- Override NotEmptyValidator translations for the Turnstile element -->
<trans-unit id="1221560910">
<source>We could not identify you as a human. Please try again.</source>
<source>The captcha challenge failed.</source>
</trans-unit>
<trans-unit id="1221560718">
<source>We could not identify you as a human. Please try again.</source>
<source>The captcha challenge failed.</source>
</trans-unit>
</body>
</file>
Expand Down
4 changes: 4 additions & 0 deletions Resources/Private/Translations/es/Main.xlf
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@
<source>This form is protected by Cloudflare Turnstile. Please enable JavaScript for this page to identify you as a human.</source>
<target state="final">Este formulario está protegido por Cloudflare Turnstile. Por favor, active JavaScript para que esta página le identifique como humano.</target>
</trans-unit>
<trans-unit id="widget.reset.btn" xml:space="preserve" approved="yes">
<source>Please click here to confirm that you are not a robot.</source>
<target state="final">Haga clic aquí para confirmar que no es un robot.</target>
</trans-unit>
</body>
</file>
</xliff>
8 changes: 4 additions & 4 deletions Resources/Private/Translations/es/ValidationErrors.xlf
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@

<!-- Override NotEmptyValidator translations for the Turnstile element -->
<trans-unit id="1221560910" xml:space="preserve" approved="yes">
<source>We could not identify you as a human. Please try again.</source>
<target state="final">No hemos podido identificarle como humano. Por favor, inténtelo de nuevo.</target>
<source>The captcha challenge failed.</source>
<target state="final">El desafío captcha ha fallado.</target>
</trans-unit>
<trans-unit id="1221560718" xml:space="preserve" approved="yes">
<source>We could not identify you as a human. Please try again.</source>
<target state="final">No hemos podido identificarle como humano. Por favor, inténtelo de nuevo.</target>
<source>The captcha challenge failed.</source>
<target state="final">El desafío captcha ha fallado.</target>
</trans-unit>
</body>
</file>
Expand Down
6 changes: 6 additions & 0 deletions Resources/Public/JavaScript/Turnstile.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,9 @@ function cfTurnstileLazyload() {
}
}
cfTurnstileLazyload();

function cfTurnstileReset(btn) {
cfTurnstileLazyload();
btn.closest('form').dispatchEvent(new Event('input'));
btn.closest('.cf-turnstile-error').style.display = 'none';
}

0 comments on commit 580f941

Please sign in to comment.