Installation:
composer require coderflex/filament-turnstile
For Filament V2, use ^1.0 version.
Configure Keys:
Add Cloudflare Turnstile keys to .env:
TURNSTILE_SITE_KEY=your_site_key
TURNSTILE_SECRET_KEY=your_secret_key
Use Cloudflare's dummy keys for testing.
First Use Case: Add the Turnstile component to a Filament form:
use Coderflex\FilamentTurnstile\Forms\Components\Turnstile;
Turnstile::make('captcha')
->theme('auto')
->language('en-US')
->size('normal');
reset-captcha for dynamic behavior (see Usage).Form Integration:
// In a Filament Form component
public function form(Form $form): Form
{
return $form
->schema([
// ... other fields
Turnstile::make('captcha')
->label('Verify You\'re Human')
->required()
->theme('dark'), // 'light', 'auto'
]);
}
Dynamic Reset on Validation Errors:
// In a Livewire form component
protected function onValidationError(ValidationException $exception): void
{
$this->dispatch('reset-captcha'); // Reset Turnstile after errors
parent::onValidationError($exception);
}
Login Page Customization: Override Filament’s default login page to include Turnstile:
// app/Filament/Pages/Auth/Login.php
class Login extends AuthLogin
{
protected function getForms(): array
{
return [
'form' => $this->form(
$this->makeForm()
->schema([
$this->getEmailFormComponent(),
$this->getPasswordFormComponent(),
Turnstile::make('captcha')->theme('auto'),
])
),
];
}
}
Global Turnstile for All Forms:
Register the component globally in AppServiceProvider:
public function boot(): void
{
Filament::registerFormComponents([
Turnstile::class,
]);
}
Conditional Rendering:
Use visible() or hidden() methods to show/hide Turnstile based on logic:
Turnstile::make('captcha')
->visible(fn ($record) => $record->isPublic())
Custom Validation:
Validate the Turnstile response in your form’s rules():
public function rules(): array
{
return [
'captcha' => 'required|turnstile',
];
}
Multi-Language Support: Dynamically set language based on user locale:
Turnstile::make('captcha')
->language(app()->getLocale())
Asset Optimization: Lazy-load Turnstile scripts (enabled by default in v2.3.1+). No manual configuration needed.
Key Mismatch:
TURNSTILE_SITE_KEY/TURNSTILE_SECRET_KEY are missing or incorrect..env and test with Cloudflare’s dummy keys first.Multiple Instances:
v2.4.1+ or manually ensure unique IDs for each instance:
Turnstile::make('captcha_1')->uniqueId('unique_id_1')
Event Dispatch Timing:
reset-captcha may not trigger if dispatched too early (e.g., before the component mounts).onValidationError or after the form is rendered.Language Format:
en-US vs. en_us) causes rendering errors.en-US, fr-FR) as per Cloudflare’s docs.Asset Conflicts:
filament-turnstile:assets blade directive if needed.Turnstile Not Rendering: Check browser console for errors. Common causes:
TURNSTILE_SITE_KEY.display: none on the container).Validation Failures:
Ensure the turnstile validation rule is registered (handled automatically by laravel-turnstile).
Event Not Firing: Verify the event is dispatched after the Turnstile component is initialized:
// Correct: Dispatch in onValidationError or mounted()
protected function mounted(): void
{
$this->dispatch('reset-captcha');
}
Custom Themes/Styles: Override Turnstile’s CSS by publishing assets:
php artisan vendor:publish --tag=filament-turnstile-assets
Then modify resources/views/vendor/filament-turnstile/styles.blade.php.
Custom Validation Logic:
Extend the turnstile rule in AppServiceProvider:
use Coderflex\LaravelTurnstile\Rules\Turnstile;
public function boot(): void
{
$this->app['validator']->extend('turnstile', function ($attribute, $value, $parameters, $validator) {
// Custom logic (e.g., IP-based restrictions)
return Turnstile::validate($value, $parameters);
});
}
Dynamic Site Key: Fetch the site key dynamically (e.g., from a config file or API):
Turnstile::make('captcha')
->siteKey(config('turnstile.site_key'))
Testing: Use Cloudflare’s test tokens in PHPUnit:
$this->post('/form', [
'captcha' => '03AHJ2-R47HLS32-5D...', // Test token
]);
Default Values:
'auto' (defaults to system preference).'en-US' (fallback to browser locale if unsupported).'normal' (use 'compact' for space-saving).Environment-Specific Keys:
Use .env.local or Laravel’s environment-based configs to manage keys per environment.
Filament V2/V3:
^1.0 version.^2.0+ (includes global registration by default).How can I help you explore Laravel packages today?