| Install | |
|---|---|
composer require elvisblanco1993/livewire-v4-recaptcha |
|
| Latest Version: | v1.0.0 |
| PHP: | ^8.2 |
A lightweight package that adds Google reCAPTCHA protection to Livewire components using a simple directive and attribute.
This package supports:
Developed and maintained by Code Wize Technologies LLC.
composer require elvisblanco1993/livewire-v4-recaptcha
Read Google's documentation to create your reCAPTCHA keys:
https://developers.google.com/recaptcha/intro
Each version requires its own site key and secret key pair.
| Version | Documentation | Notes |
|---|---|---|
| v3 (recommended) | https://developers.google.com/recaptcha/docs/v3 | Score-based verification |
| v2 | https://developers.google.com/recaptcha/docs/display | Standard checkbox |
| v2 Invisible | https://developers.google.com/recaptcha/docs/invisible | Use 'size' => 'invisible' |
Add the configuration to config/services.php
'google' => [
'recaptcha' => [
'site_key' => env('GOOGLE_RECAPTCHA_SITE_KEY'),
'secret_key' => env('GOOGLE_RECAPTCHA_SECRET_KEY'),
'version' => 'v3',
'score' => 0.5,
],
],
score must be between 0 and 1 and determines the minimum trust threshold.
'google' => [
'recaptcha' => [
'site_key' => env('GOOGLE_RECAPTCHA_SITE_KEY'),
'secret_key' => env('GOOGLE_RECAPTCHA_SECRET_KEY'),
'version' => 'v2',
'size' => 'normal',
'theme' => 'light',
],
],
Available options:
size: normal | compact | invisible
theme: light | dark
Add the #[ValidatesRecaptcha] attribute to the method that processes the form.
use Livewire\Component;
use ElvisBlanco1993\LivewireRecaptcha\ValidatesRecaptcha;
class ContactForm extends Component
{
public string $gRecaptchaResponse;
#[ValidatesRecaptcha]
public function submit()
{
// This only runs if the captcha passes
}
}
If the $gRecaptchaResponse property is not declared, it will be automatically created.
You can override the secret key or score directly:
#[ValidatesRecaptcha(secretKey: 'mysecretkey', score: 0.9)]
This is useful if you want different captcha strictness per form.
Attach the directive to the form you want protected.
<form wire:submit="submit" wire:recaptcha>
<!-- form fields -->
<button type="submit">
Submit
</button>
</form>
Add the Blade directive once on the page:
@livewireRecaptcha
You can add it:
When verification fails, a validation error is thrown for:
gRecaptchaResponse
Example:
@if ($errors->has('gRecaptchaResponse'))
<div class="text-red-600">
{{ $errors->first('gRecaptchaResponse') }}
</div>
@endif
The translation key is:
livewire-recaptcha::recaptcha.invalid_response
You can override any configuration directly:
@livewireRecaptcha(
version: 'v2',
siteKey: 'your_site_key',
theme: 'dark',
size: 'compact'
)
wire:recaptcha directive intercepts the request.If validation fails, the request is rejected.
| Package | Supported |
|---|---|
| Laravel | 10+ |
| Livewire | v3 |
| Livewire | v4 |
Always validate reCAPTCHA server-side, which this package does automatically.
Never trust client responses alone.
MIT License