Skip to content

Commit f2709bf

Browse files
[2.x] Adds Livewire and Filament PHP helpers.
1 parent 2be0e4c commit f2709bf

12 files changed

Lines changed: 1750 additions & 44 deletions

File tree

.gitattributes

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,4 @@
1010
/phpunit.xml export-ignore
1111
/tests export-ignore
1212
/.editorconfig export-ignore
13+
/phpstan.neon export-ignore

.github/workflows/php.yml

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,9 +62,6 @@ jobs:
6262
- 13.*
6363
dependencies: [ lowest, highest ]
6464
include-filament: [ true ]
65-
exclude:
66-
- laravel-constraint: "13.*"
67-
include-filament: true
6865

6966
steps:
7067
- name: Set up PHP

README.md

Lines changed: 77 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ Finally, you can also set a custom callback name to be executed once the script
107107

108108
#### Site Key on JavaScript frontend
109109

110-
If you put the script on the `<head>` part of your HTML view, you may set the `meta` attribute to render a `<meta>` tag alongside the script. This tag will contain your Turnstile site key so your JavaScript frontend can retrieve use it to render the widget.
110+
If you put the script on the `<head>` part of your HTML view, you may set the `meta` attribute to render a `<meta>` tag alongside the script. This tag will contain your Turnstile site-key so your JavaScript frontend can use it to render the widget.
111111

112112
```blade
113113
<!DOCTYPE html>
@@ -161,11 +161,24 @@ Alternatively, you may set a custom name for the tag by setting a value to the `
161161
<meta name="my-custom-key" content="1x00000000000000000000AA" />
162162
```
163163

164+
#### Preconnect
165+
166+
You can also add a [resource hint to Cloudflare servers](https://developers.cloudflare.com/turnstile/get-started/client-side-rendering/#2-optional-optimize-performance-with-resource-hints) by setting the `preconnect` attribute in the component.
167+
168+
```blade
169+
<x-turnstile::script preconnect />
170+
```
171+
172+
```html
173+
<script src="https://challenges.cloudflare.com/turnstile/v0/api.js" defer async></script>
174+
<link rel="preconnect" href="https://challenges.cloudflare.com">
175+
```
176+
164177
### Widget
165178

166179
> [!IMPORTANT]
167180
>
168-
> Remember that the Widget Mode is controlled via your [Cloudflare Dashboard](https://dash.cloudflare.com), not here. On development, this is controlled with [testing keys](#testing-keys).
181+
> Remember that the Widget Mode is controlled via your [Cloudflare Dashboard](https://dash.cloudflare.com), not here. In development, this is controlled with [testing keys](#testing-keys).
169182
170183
You can use the `<x-turnstile::widget />` Blade Component to add the Turnstile Widget in your forms. Depending on the Widget Mode, the Widget may render as usual or be invisible at Turnstile discretion.
171184

@@ -840,12 +853,14 @@ return Application::configure(basePath: dirname(__DIR__))
840853
->create();
841854
```
842855
843-
## Livewire & Filament trait
856+
## Livewire Trait
844857

845-
If you're using Liveware or Filament PHP, you may use the `Laragear\Turnstile\Livewire\InteractsWithTurnstile` trait in your Filament Pages or components.
858+
If you're using Liveware, you may use the `Laragear\Turnstile\Livewire\InteractsWithTurnstile` trait in your Livewire pages or components.
846859

847860
The trait will register a hook _after validation_, that only runs on non-Precognitive requests. This way, the Turnstile Challenge is consumed only when the form is completely validated. It also avoids using an idempotency key based on the request fingerprint, saving you a request to Cloudflare Turnstile servers.
848861

862+
For example, you can use it on Filament custom pages to handle form submission.
863+
849864
```php
850865
use Filament\Forms\Concerns\InteractsWithForms;
851866
use Filament\Forms\Contracts\HasForms;
@@ -881,10 +896,6 @@ protected function turnstileToken(string $key)
881896
}
882897
```
883898

884-
> [!IMPORTANT]
885-
>
886-
> The trait injects the Turnstile challenge validation on all forms. If you need more customization, use the [`afterValidate()` hook](https://filamentphp.com/docs/5.x/resources/creating-records#lifecycle-hooks).
887-
888899
### Handling the Turnstile Challenge
889900

890901
You have access to some useful methods to handle if the challenge should be deemed successful or not, and how to handle successes and failures:
@@ -917,6 +928,64 @@ protected function handleTurnstileChallengeStatus(Challenge $challenge): bool
917928
}
918929
```
919930

931+
## Filament Widget Field
932+
933+
If you're using Filament, you may use the `Laragear\Turnstile\Filament\Forms\TurnstileWidget` field in your forms. It will automatically inject the Turnstile Script in the page and render the Turnstile Widget. The Turnstile Challenge will be validated only on complete form submission.
934+
935+
```php
936+
use Filament\Forms\Components\Textarea;
937+
use Filament\Forms\Components\TextInput;
938+
use Filament\Forms\Concerns\InteractsWithForms;
939+
use Filament\Forms\Contracts\HasForms;
940+
use Filament\Pages\Page;
941+
use Filament\Schemas\Schema;
942+
use Laragear\Turnstile\Filament\Forms\TurnstileWidget;
943+
use Laragear\Turnstile\Livewire\InteractsWithTurnstile;
944+
945+
class ContactPage extends Page implements HasForms
946+
{
947+
use InteractsWithForms;
948+
use InteractsWithTurnstile;
949+
950+
public function form(Schema $schema) : Schema
951+
{
952+
return $schema->components([
953+
TextInput::make('subject')->required(),
954+
Textarea::make('message')->required(),
955+
// Add the Turnstile Widget
956+
TurnstileWidget::make(),
957+
]);
958+
}
959+
}
960+
```
961+
962+
The Widget includes some convenient methods based on the [Cloudflare Turnstile Widget configuration](https://developers.cloudflare.com/turnstile/get-started/client-side-rendering/widget-configurations/):
963+
964+
| Method | Description |
965+
|------------------------------------|------------------------------------------------------------------------|
966+
| `normal()` | Sets the size of the widget to normal. |
967+
| `flexible()` | Sets the size of the widget to flexible. |
968+
| `compact()` | Sets the size of the widget to compact. |
969+
| `system()` | Sets the theme of the widget to system/browser default. |
970+
| `light()` | Sets the theme of the widget to light. |
971+
| `dark()` | Sets the theme of the widget to dark. |
972+
| `appearanceAlways()` | Sets the appearance of the widget to always appear. |
973+
| `appearanceExecute()` | Sets the appearance of the widget to appear on manual execution. |
974+
| `appearanceInteractionOnly()` | Sets the appearance of the widget to appear only when required. |
975+
| `executionRender()` | Sets the execution of the widget challenge as it renders. |
976+
| `executionExecute()` | Sets the execution of the widget challenge on manual execution. |
977+
| `languageAuto()` | Sets the language of the widget to browser locale. |
978+
| `language(string $lang)` | Sets the language of the widget to given locale. |
979+
| `languageApp()` | Sets the language of the widget to application locale. |
980+
| `tabindex(int $tabindex)` | Sets the tab order of the widget to one set. |
981+
| `callback(string $functionName)` | Adds an additional function names to execute on successful challenges. |
982+
| `actionName(string $functionName)` | Sets the action name for the challenge. |
983+
| `implicit(bool $condition = true)` | Makes the widget be rendered automatically by the script. |
984+
985+
> [!IMPORTANT]
986+
>
987+
> The Filament Turnstile Widget is rendered **implicitly** by default, meaning its rendering is handled internally by AlpineJS. This makes all the options to be passed as a JavaScript object. You can use **explicit** rendering, but you may have problems if Livewire or Filament are configured to run in [SPA mode](https://filamentphp.com/docs/5.x/panel-configuration#spa-mode) or [use islands](https://livewire.laravel.com/docs/4.x/islands).
988+
920989
## Advanced configuration
921990

922991
Laragear Turnstile is intended to work out-of-the-box, but you can publish the configuration file for fine-tuning the Challenge verification.

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535
"illuminate/session": "12.*|13.*"
3636
},
3737
"require-dev": {
38-
"filament/filament": "4.*|5.*",
38+
"filament/filament": "5.*",
3939
"orchestra/testbench": "10.*|11.*"
4040
},
4141
"autoload": {

phpstan.neon

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
parameters:
2+
ignoreErrors:
3+
- '#Trait Laragear\\Turnstile\\Livewire\\InteractsWithTurnstile is used zero times and is not analysed\.#'

0 commit comments

Comments
 (0)