Basic Usage

Turnstile Widget Component

Since this package, it is so simple to use. Once you require it, you can use the turnstile widget in your form, like the following

1<x-turnstile-widget
2 theme="dark"
3 language="en-US"
4 size="normal"
5 callback="callbackFunction"
6 errorCallback="errorCallbackFunction"
7/>

As you can see, the widget has few options to use. You can know more about them in the configuration section

Turnstile Backend Validation

Once you used the widget component, in the frontend. You can validate Cloudflare Response, by using the validate method.

Here's an example:

1use Coderflex\LaravelTurnstile\Facades\LaravelTurnstile;
2 
3public function store(Request $request)
4{
5 // maybe you want to validate your form first
6 
7 $response = LaravelTurnstile::validate();
8 
9 
10 if (! $response['success']) { // will return boolean
11 // do your logic
12 }
13}

You may, optionally, send the Cloudflare response with the validation method. Something like the following:

1public function store(Request $request)
2{
3 ...
4 $response = LaravelTurnstile::validate(
5 $request->get('cf-turnstile-response'); // this will be created from the cloudflare widget.
6 );
7 ...
8}

Turnstile Custom Rule

If you want clean validation, you can use the TurnstileCheck custom rule, along with your form validation. Here's an example:

1use Coderflex\LaravelTurnstile\Rules\TurnstileCheck;
2 
3public function store(Request $request)
4{
5 $request->validate([
6 'cf-turnstile-response' => [new TurnstileCheck()]
7 ]);
8}

The custom rule, will use the same logic, as the backend validation, but instead will check for the response, and return a validation message, if the captcha fails.

You can change the content of the validation message, in config/turnstile.php file

1return [
2 ...
3 'error_messages' => [
4 'turnstile_check_message' => 'The CAPTCHA thinks you are a robot! Please refresh and try again.',
5 ],
6];

PS: If you want to translate the message, just copy the message and translate it, because it uses the translator method behind the scene.

Real Life Example

In your blade file

1<form action="" method="post">
2 @csrf
3 <div>
4 <input type="text" name="name" />
5 @error('name')
6 <p class="error">{{ $message }}</p>
7 @enderror
8 </div>
9 
10 <div>
11 <x-turnstile-widget theme="auto" language="fr"/>
12 @error('cf-turnstile-response')
13 <p class="error">{{ $message }}</p>
14 @enderror
15 </div>
16 
17 <button>Submit</button>
18</form>

In your controller:

1use Coderflex\LaravelTurnstile\Rules\TurnstileCheck;
2use Coderflex\LaravelTurnstile\Facades\LaravelTurnstile;
3 
4...
5 
6public function store(Request $request)
7{
8 $request->validate([
9 'name' => ['required', 'string', 'max:250'],
10 'cf-turnstile-response' => ['required', new TurnstileCheck()],
11 ]);
12 
13 // or
14 $response = LaravelTurnstile::validate();
15 
16 if (! $response['success']) {
17 // do your thing.
18 }
19 
20 // do your things.
21}

Join our newsletter

Subscribe to Our Newsletter and never miss our offers, latest news, Articles, etc.

We care about the protection of your data. Read our Privacy Policy.