excelwebzone/recaptcha-bundle
Symfony bundle that adds Google reCAPTCHA (v2 and v3) form field integration with simple configuration for keys, locale/options, enable/disable per environment, custom API host for regions like China, and optional HTTP proxy support.
Installation:
composer require excelwebzone/recaptcha-bundle
For Symfony 4/5, no AppKernel.php registration is needed—autoloading handles it.
Configuration:
Add to config/packages/ewz_recaptcha.yaml:
ewz_recaptcha:
version: 3 # or 2
site_key: 'YOUR_SITE_KEY'
secret_key: 'YOUR_SECRET_KEY'
For dev environments, override in config/packages/dev/ewz_recaptcha.yaml.
First Use Case: Add the Twig extension to a form template:
{{ render_recaptcha('contact_form') }}
Validate in a controller:
use EWZ\Bundle\RecaptchaBundle\Validator\Constraints\Recaptcha;
public function submitForm(Request $request) {
$form = $this->createForm(...);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
// Success
}
}
Twig Integration:
Use render_recaptcha() in templates with optional parameters:
{{ render_recaptcha('form_name', {
'theme': 'light',
'size': 'compact',
'type': 'image' # v2 only
}) }}
FormBuilder Integration: Add the constraint to a form field:
$builder->add('submit', SubmitType::class, [
'constraints' => [
new Recaptcha([
'version' => 3,
'error_message' => 'Please verify you are not a robot.'
])
]
]);
Dynamic Configuration: Override settings per form/action via YAML:
ewz_recaptcha:
forms:
contact_form:
version: 3
size: 'normal'
Multi-Step Forms:
Reuse the same site_key across steps but validate only on submission.
API/Non-Twig Use: Validate manually in controllers:
$validator = $this->container->get('ewz_recaptcha.validator');
$isValid = $validator->validate($request->request->get('g-recaptcha-response'));
Custom Error Handling: Extend the validator or override Twig error messages:
{% if form.errors.hasError('submit', 'recaptcha') %}
<div class="error">{{ form_errors(form.submit) }}</div>
{% endif %}
Version Mismatch:
secret_key vs. secret).action in the frontend script (auto-handled by the bundle).Caching Issues:
site_key/secret_key:
php bin/console cache:clear
Dev Environment:
localhost in allowed_domains for testing (v3 only):
ewz_recaptcha:
allowed_domains: ['localhost', '127.0.0.1']
Validation Failures:
03AHJ2... (valid) vs. empty string (invalid).Logs: Enable debug mode to log validation attempts:
ewz_recaptcha:
debug: true
Custom Validators:
Extend EWZ\Bundle\RecaptchaBundle\Validator\RecaptchaValidator for custom logic.
Twig Extensions:
Override render_recaptcha() in a custom Twig extension for theming.
Event Listeners:
Subscribe to ewz_recaptcha.validate events for pre/post-validation hooks.
ewz_recaptcha:
version: 3
score_threshold: 0.5 # Default is 0.9
hl parameter in Twig for language-specific prompts:
{{ render_recaptcha('form', {'hl': 'es'}) }}
How can I help you explore Laravel packages today?