-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
e4e965d
commit edcafc0
Showing
13 changed files
with
395 additions
and
206 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,45 +1,74 @@ | ||
type RecaptchaApi = { | ||
render: (elementId: string, options: Record<string, unknown>) => number; | ||
reset: (widgetId: number) => void; | ||
}; | ||
|
||
type Config = { | ||
siteKey: string; | ||
scriptSrc: string; | ||
formControlId: string; | ||
type: "grecaptcha" | "hcaptcha"; | ||
onLoadCallbackName: string; | ||
}; | ||
|
||
interface Window { | ||
[key: string]: any; | ||
|
||
grecaptcha: { | ||
render: (elementId: string, options: Record<string, unknown>) => number; | ||
reset: (widgetId: number) => void; | ||
}; | ||
grecaptcha: RecaptchaApi; | ||
hcaptcha: RecaptchaApi; | ||
} | ||
|
||
document.addEventListener("DOMContentLoaded", () => { | ||
document.querySelectorAll<HTMLFormElement>(".palmtree-form").forEach((form) => { | ||
const element = form.querySelector<HTMLElement>(".g-recaptcha-autoload"); | ||
|
||
if (element && element.dataset.onload && element.dataset.script_url) { | ||
window[element.dataset.onload] = () => { | ||
const widgetId = window.grecaptcha.render(element.id, { | ||
sitekey: element.dataset.site_key, | ||
callback: (response: string) => { | ||
const formControl = document.querySelector<HTMLInputElement>(`#${element.dataset.form_control}`); | ||
if (formControl) { | ||
formControl.value = response; | ||
|
||
if (form.palmtreeForm("isInitialized")) { | ||
form.palmtreeForm("clearState", formControl); | ||
} | ||
const element = form.querySelector<HTMLElement>(".palmtree-captcha-autoload"); | ||
|
||
if (!element) { | ||
return; | ||
} | ||
|
||
const config = JSON.parse(element.dataset.palmtreeFormCaptcha || "") as Config; | ||
|
||
if (!config) { | ||
console.error("Invalid configuration"); | ||
return; | ||
} | ||
|
||
if (config.type !== "grecaptcha" && config.type !== "hcaptcha") { | ||
console.error(`Captcha type ${config.type} is not supported.`); | ||
return; | ||
} | ||
|
||
window[config.onLoadCallbackName] = () => { | ||
const api = window[config.type]; | ||
|
||
const widgetId = api.render(element.id, { | ||
sitekey: config.siteKey, | ||
callback: (response: string) => { | ||
const formControl = document.querySelector<HTMLInputElement>(`#${config.formControlId}`); | ||
if (formControl) { | ||
formControl.value = response; | ||
|
||
const palmtreeForm = window.palmtreeForm.getInstance(form); | ||
|
||
if (palmtreeForm) { | ||
palmtreeForm.clearState([formControl]); | ||
} | ||
}, | ||
}); | ||
} | ||
}, | ||
}); | ||
|
||
["error", "success"].forEach((event) => { | ||
form.addEventListener(`${event}.palmtreeForm`, () => { | ||
window.grecaptcha.reset(widgetId); | ||
}); | ||
["error", "success"].forEach((event) => { | ||
form.addEventListener(`palmtreeForm.${event}`, () => { | ||
api.reset(widgetId); | ||
}); | ||
}; | ||
}); | ||
}; | ||
|
||
const script = document.createElement("script"); | ||
script.async = true; | ||
script.defer = true; | ||
script.src = element.dataset.script_url; | ||
const script = document.createElement("script"); | ||
script.async = true; | ||
script.defer = true; | ||
script.src = config.scriptSrc; | ||
|
||
document.head.append(script); | ||
} | ||
document.head.append(script); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
<?php declare(strict_types=1); | ||
|
||
use Palmtree\Form\Captcha\GoogleRecaptcha; | ||
use Palmtree\Form\Captcha\HCaptcha; | ||
use Palmtree\Form\FormBuilder; | ||
use Palmtree\Form\Type\TextType; | ||
|
||
require __DIR__ . '/../../vendor/autoload.php'; | ||
require __DIR__ . '/../.bootstrap.php'; | ||
|
||
$builder = new FormBuilder([ | ||
'key' => 'hcaptcha', | ||
'html_validation' => false, | ||
]); | ||
|
||
$builder | ||
->add('name', TextType::class, [ | ||
'label' => 'Please enter your name', | ||
]) | ||
->add('hcaptcha', 'captcha', [ | ||
'captcha' => new HCaptcha('6b1ef180-ed78-4948-ae66-258e0bfe4ecc', 'ES_c1935238614149509f69db166c2f970d'), | ||
]); | ||
|
||
$builder->add('send_message', 'submit'); | ||
|
||
$form = $builder->getForm(); | ||
|
||
$form->handleRequest(); | ||
|
||
if ($form->isSubmitted() && $form->isValid()) { | ||
redirect('?success=1'); | ||
} | ||
|
||
$view = template('view.phtml', [ | ||
'form' => $form, | ||
'success' => (!empty($_GET['success'])), | ||
]); | ||
|
||
echo $view; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
<!doctype html> | ||
<html> | ||
<head> | ||
<meta charset="utf-8"> | ||
<title>Palmtree Form - HCaptcha Example</title> | ||
<?= get_styles(); ?> | ||
</head> | ||
<body> | ||
<main> | ||
<div class="container mt-3"> | ||
<h1>Palmtree Form - HCaptcha Example</h1> | ||
<?php if ($success): ?> | ||
<div class="alert alert-success">Form submitted successfully</div> | ||
<?php endif; ?> | ||
<?= $form->render(); ?> | ||
</div> | ||
</main> | ||
<?= get_scripts(); ?> | ||
<script src="/dist/palmtree-form.pkgd.js"></script> | ||
</body> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.