Skip to content

Commit

Permalink
feat: add turnstile validation rule and usage example in README
Browse files Browse the repository at this point in the history
  • Loading branch information
200-0K committed Dec 24, 2024
1 parent 7cd0dbf commit 9faa4fe
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 0 deletions.
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,19 @@ public function submit(Request $request)
}
```

Or simply use the `turnstile` rule directly.

```php
use RyanChandler\LaravelCloudflareTurnstile\Rules\Turnstile;

public function submit(Request $request)
{
$request->validate([
'cf-turnstile-response' => ['required', 'turnstile'],
]);
}
```

### Customizing the widget

You can customize the widget by passing attributes to the `<x-turnstile />` component.
Expand Down
16 changes: 16 additions & 0 deletions src/LaravelCloudflareTurnstileServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace RyanChandler\LaravelCloudflareTurnstile;

use Illuminate\Support\Facades\Blade;
use Illuminate\Support\Facades\Validator;
use Illuminate\Validation\Rule;
use RyanChandler\LaravelCloudflareTurnstile\Rules\Turnstile;
use RyanChandler\LaravelCloudflareTurnstile\View\Components\Turnstile as TurnstileComponent;
Expand Down Expand Up @@ -36,5 +37,20 @@ public function packageBooted()
Rule::macro('turnstile', function () {
return app(Turnstile::class);
});

Validator::extend(
'turnstile',
function ($attribute, $value, $parameters, $validator) {
$rule = app(Turnstile::class);
$isPassed = $rule->passes($attribute, $value);
if ($isPassed) {
return true;
}

$validator->setCustomMessages([
$attribute => $rule->message()[0],
]);
}
);
}
}

0 comments on commit 9faa4fe

Please sign in to comment.