Skip to content

Commit 9faa4fe

Browse files
committed
feat: add turnstile validation rule and usage example in README
1 parent 7cd0dbf commit 9faa4fe

File tree

2 files changed

+29
-0
lines changed

2 files changed

+29
-0
lines changed

README.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,19 @@ public function submit(Request $request)
101101
}
102102
```
103103

104+
Or simply use the `turnstile` rule directly.
105+
106+
```php
107+
use RyanChandler\LaravelCloudflareTurnstile\Rules\Turnstile;
108+
109+
public function submit(Request $request)
110+
{
111+
$request->validate([
112+
'cf-turnstile-response' => ['required', 'turnstile'],
113+
]);
114+
}
115+
```
116+
104117
### Customizing the widget
105118

106119
You can customize the widget by passing attributes to the `<x-turnstile />` component.

src/LaravelCloudflareTurnstileServiceProvider.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace RyanChandler\LaravelCloudflareTurnstile;
44

55
use Illuminate\Support\Facades\Blade;
6+
use Illuminate\Support\Facades\Validator;
67
use Illuminate\Validation\Rule;
78
use RyanChandler\LaravelCloudflareTurnstile\Rules\Turnstile;
89
use RyanChandler\LaravelCloudflareTurnstile\View\Components\Turnstile as TurnstileComponent;
@@ -36,5 +37,20 @@ public function packageBooted()
3637
Rule::macro('turnstile', function () {
3738
return app(Turnstile::class);
3839
});
40+
41+
Validator::extend(
42+
'turnstile',
43+
function ($attribute, $value, $parameters, $validator) {
44+
$rule = app(Turnstile::class);
45+
$isPassed = $rule->passes($attribute, $value);
46+
if ($isPassed) {
47+
return true;
48+
}
49+
50+
$validator->setCustomMessages([
51+
$attribute => $rule->message()[0],
52+
]);
53+
}
54+
);
3955
}
4056
}

0 commit comments

Comments
 (0)