From 15e0146bcc3df6716e015956cdc0d8c44c91582b Mon Sep 17 00:00:00 2001 From: HUSAM TAREQ AL-SHAIBANI Date: Thu, 13 Oct 2022 00:14:00 +0300 Subject: [PATCH 01/11] Make Resource Macroable --- packages/admin/src/Resources/Resource.php | 3 +++ 1 file changed, 3 insertions(+) diff --git a/packages/admin/src/Resources/Resource.php b/packages/admin/src/Resources/Resource.php index a3d2b9a209..db4de80c6c 100644 --- a/packages/admin/src/Resources/Resource.php +++ b/packages/admin/src/Resources/Resource.php @@ -16,9 +16,12 @@ use Illuminate\Support\Collection; use Illuminate\Support\Facades\Gate; use Illuminate\Support\Facades\Route; use Illuminate\Support\Str; +use Illuminate\Support\Traits\Macroable; class Resource { + use Macroable { __call as macroCall; } + protected static ?string $breadcrumb = null; protected static bool $isGloballySearchable = true; From 71f60a6dcf426d2cb65e0f8d5b7d2dd3ea7b6fa8 Mon Sep 17 00:00:00 2001 From: jim Date: Thu, 13 Oct 2022 13:23:52 +0100 Subject: [PATCH 02/11] Allow the tel regex to be overridden. --- packages/forms/docs/03-fields.md | 18 ++++++++++++++++++ packages/forms/src/Components/TextInput.php | 16 +++++++++++++++- 2 files changed, 33 insertions(+), 1 deletion(-) diff --git a/packages/forms/docs/03-fields.md b/packages/forms/docs/03-fields.md index dfe6158695..279955d563 100644 --- a/packages/forms/docs/03-fields.md +++ b/packages/forms/docs/03-fields.md @@ -316,6 +316,24 @@ TextInput::make('password') For more complex autocomplete options, text inputs also support [datalists](#datalists). +#### Telephone validation + +When using a tel field the value will be validated using this default regex `/^[+]*[(]{0,1}[0-9]{1,4}[)]{0,1}[-\s\.\/0-9]*$/`. If you wish to override that you can use the `telRegex()` method. + +```php +use Filament\Forms\Components\TextInput; + +// Per field +TextInput::make('telephone') + ->tel() + ->telRegex('/^[+]*[(]{0,1}[0-9]{1,4}[)]{0,1}[-\s\.\/0-9]*$/'); + +// Globally +TextInput::configureUsing(function (TextInput $input): void { + $input->telRegex('/^[+]*[(]{0,1}[0-9]{1,4}[)]{0,1}[-\s\.\/0-9]*$/'); +}); +``` + ### Affixes You may place text before and after the input using the `prefix()` and `suffix()` methods: diff --git a/packages/forms/src/Components/TextInput.php b/packages/forms/src/Components/TextInput.php index 4a61edd7d3..431dcc0f83 100644 --- a/packages/forms/src/Components/TextInput.php +++ b/packages/forms/src/Components/TextInput.php @@ -40,6 +40,8 @@ class TextInput extends Field implements Contracts\CanBeLengthConstrained, CanHa protected $minValue = null; + protected string | Closure $telRegex = '/^[+]*[(]{0,1}[0-9]{1,4}[)]{0,1}[-\s\.\/0-9]*$/'; + protected string | Closure | null $type = null; public function currentPassword(bool | Closure $condition = true): static @@ -129,7 +131,14 @@ class TextInput extends Field implements Contracts\CanBeLengthConstrained, CanHa { $this->isTel = $condition; - $this->regex(static fn (TextInput $component) => $component->evaluate($condition) ? '/^[+]*[(]{0,1}[0-9]{1,4}[)]{0,1}[-\s\.\/0-9]*$/' : null); + $this->regex(static fn (TextInput $component) => $component->evaluate($condition) ? $component->getTelRegex() : null); + + return $this; + } + + public function telRegex(string | Closure $regex): static + { + $this->telRegex = $regex; return $this; } @@ -206,6 +215,11 @@ class TextInput extends Field implements Contracts\CanBeLengthConstrained, CanHa return 'text'; } + public function getTelRegex(): string + { + return $this->evaluate($this->telRegex); + } + public function hasMask(): bool { return $this->configureMaskUsing !== null; From 1205c33e2cd83055b4cbb64c3914c972c7dc1e02 Mon Sep 17 00:00:00 2001 From: Ryan Chandler Date: Thu, 13 Oct 2022 15:16:06 +0100 Subject: [PATCH 03/11] fix: give computed properties a higher precedence than forms --- .../forms/src/Concerns/InteractsWithForms.php | 25 ++++++++----- tests/src/Forms/ComputedPropertyTest.php | 37 +++++++++++++++++++ 2 files changed, 52 insertions(+), 10 deletions(-) create mode 100644 tests/src/Forms/ComputedPropertyTest.php diff --git a/packages/forms/src/Concerns/InteractsWithForms.php b/packages/forms/src/Concerns/InteractsWithForms.php index 93ca726ac8..8ea947988e 100644 --- a/packages/forms/src/Concerns/InteractsWithForms.php +++ b/packages/forms/src/Concerns/InteractsWithForms.php @@ -3,12 +3,13 @@ namespace Filament\Forms\Concerns; use Closure; -use Filament\Forms\ComponentContainer; +use Livewire\WithFileUploads; use Illuminate\Contracts\View\View; +use Livewire\TemporaryUploadedFile; +use Filament\Forms\ComponentContainer; use Illuminate\Database\Eloquent\Model; use Illuminate\Validation\ValidationException; -use Livewire\TemporaryUploadedFile; -use Livewire\WithFileUploads; +use Livewire\Exceptions\PropertyNotFoundException; trait InteractsWithForms { @@ -27,15 +28,19 @@ trait InteractsWithForms public function __get($property) { - if ((! $this->isCachingForms) && $form = $this->getCachedForm($property)) { - return $form; - } + try { + return parent::__get($property); + } catch (PropertyNotFoundException $e) { + if ((! $this->isCachingForms) && $form = $this->getCachedForm($property)) { + return $form; + } - if ($property === 'modal') { - return $this->getModalViewOnce(); - } + if ($property === 'modal') { + return $this->getModalViewOnce(); + } - return parent::__get($property); + throw $e; + } } protected function getModalViewOnce(): ?View diff --git a/tests/src/Forms/ComputedPropertyTest.php b/tests/src/Forms/ComputedPropertyTest.php new file mode 100644 index 0000000000..f4fbba65d3 --- /dev/null +++ b/tests/src/Forms/ComputedPropertyTest.php @@ -0,0 +1,37 @@ +getTheSchema() + ->toBeArray() + ->sequence( + fn (Expectation $expect) => $expect->toBeInstanceOf(TextInput::class) + ); +}); + +class ComputedPropertySchema extends Livewire +{ + public function getFormSchema(): array + { + return $this->schema; + } + + public function getSchemaProperty() + { + return [ + TextInput::make(''), + ]; + } + + public function getTheSchema() + { + return $this->schema; + } +} From 8ef7b822de72e583dc3e4133d06859b63e636a1a Mon Sep 17 00:00:00 2001 From: ryangjchandler Date: Thu, 13 Oct 2022 14:18:25 +0000 Subject: [PATCH 04/11] chore: fix code style --- packages/forms/src/Concerns/InteractsWithForms.php | 6 +++--- tests/src/Forms/ComputedPropertyTest.php | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/packages/forms/src/Concerns/InteractsWithForms.php b/packages/forms/src/Concerns/InteractsWithForms.php index 8ea947988e..3a0c658c5d 100644 --- a/packages/forms/src/Concerns/InteractsWithForms.php +++ b/packages/forms/src/Concerns/InteractsWithForms.php @@ -3,13 +3,13 @@ namespace Filament\Forms\Concerns; use Closure; -use Livewire\WithFileUploads; -use Illuminate\Contracts\View\View; -use Livewire\TemporaryUploadedFile; use Filament\Forms\ComponentContainer; +use Illuminate\Contracts\View\View; use Illuminate\Database\Eloquent\Model; use Illuminate\Validation\ValidationException; use Livewire\Exceptions\PropertyNotFoundException; +use Livewire\TemporaryUploadedFile; +use Livewire\WithFileUploads; trait InteractsWithForms { diff --git a/tests/src/Forms/ComputedPropertyTest.php b/tests/src/Forms/ComputedPropertyTest.php index f4fbba65d3..a2714da323 100644 --- a/tests/src/Forms/ComputedPropertyTest.php +++ b/tests/src/Forms/ComputedPropertyTest.php @@ -1,8 +1,8 @@ Date: Thu, 13 Oct 2022 15:23:31 +0100 Subject: [PATCH 05/11] Update InteractsWithForms.php --- packages/forms/src/Concerns/InteractsWithForms.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/forms/src/Concerns/InteractsWithForms.php b/packages/forms/src/Concerns/InteractsWithForms.php index 3a0c658c5d..1277e35f5f 100644 --- a/packages/forms/src/Concerns/InteractsWithForms.php +++ b/packages/forms/src/Concerns/InteractsWithForms.php @@ -30,7 +30,7 @@ trait InteractsWithForms { try { return parent::__get($property); - } catch (PropertyNotFoundException $e) { + } catch (PropertyNotFoundException $exception) { if ((! $this->isCachingForms) && $form = $this->getCachedForm($property)) { return $form; } @@ -39,7 +39,7 @@ trait InteractsWithForms return $this->getModalViewOnce(); } - throw $e; + throw $exception; } } From 88ef08517af3edb80b0af6e7a12f9ffdee6a4f29 Mon Sep 17 00:00:00 2001 From: Dan Harrin Date: Thu, 13 Oct 2022 15:25:26 +0100 Subject: [PATCH 06/11] Update ComputedPropertyTest.php --- tests/src/Forms/ComputedPropertyTest.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/src/Forms/ComputedPropertyTest.php b/tests/src/Forms/ComputedPropertyTest.php index a2714da323..c40f67252f 100644 --- a/tests/src/Forms/ComputedPropertyTest.php +++ b/tests/src/Forms/ComputedPropertyTest.php @@ -9,7 +9,7 @@ uses(TestCase::class); test('computed properties used to generate form schema can be accessed before caching forms', function () { expect(ComputedPropertySchema::make()) - ->getTheSchema() + ->getSchema() ->toBeArray() ->sequence( fn (Expectation $expect) => $expect->toBeInstanceOf(TextInput::class) @@ -30,7 +30,7 @@ class ComputedPropertySchema extends Livewire ]; } - public function getTheSchema() + public function getSchema() { return $this->schema; } From 8123cda96bbab3d4e8f282bf815e166c19b56f0c Mon Sep 17 00:00:00 2001 From: Dan Harrin Date: Thu, 13 Oct 2022 15:33:06 +0100 Subject: [PATCH 07/11] Update TextInput.php --- packages/forms/src/Components/TextInput.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/packages/forms/src/Components/TextInput.php b/packages/forms/src/Components/TextInput.php index 431dcc0f83..e4465aeb8c 100644 --- a/packages/forms/src/Components/TextInput.php +++ b/packages/forms/src/Components/TextInput.php @@ -40,7 +40,7 @@ class TextInput extends Field implements Contracts\CanBeLengthConstrained, CanHa protected $minValue = null; - protected string | Closure $telRegex = '/^[+]*[(]{0,1}[0-9]{1,4}[)]{0,1}[-\s\.\/0-9]*$/'; + protected string | Closure | null $telRegex = null; protected string | Closure | null $type = null; @@ -136,7 +136,7 @@ class TextInput extends Field implements Contracts\CanBeLengthConstrained, CanHa return $this; } - public function telRegex(string | Closure $regex): static + public function telRegex(string | Closure | null $regex): static { $this->telRegex = $regex; @@ -217,7 +217,7 @@ class TextInput extends Field implements Contracts\CanBeLengthConstrained, CanHa public function getTelRegex(): string { - return $this->evaluate($this->telRegex); + return $this->evaluate($this->telRegex) ?? '/^[+]*[(]{0,1}[0-9]{1,4}[)]{0,1}[-\s\.\/0-9]*$/'; } public function hasMask(): bool From 4511e35c27452e55630b2c1127e542a25591acc9 Mon Sep 17 00:00:00 2001 From: Dan Harrin Date: Thu, 13 Oct 2022 15:34:56 +0100 Subject: [PATCH 08/11] Update 03-fields.md --- packages/forms/docs/03-fields.md | 20 +++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) diff --git a/packages/forms/docs/03-fields.md b/packages/forms/docs/03-fields.md index 279955d563..2acaca3afd 100644 --- a/packages/forms/docs/03-fields.md +++ b/packages/forms/docs/03-fields.md @@ -318,19 +318,25 @@ For more complex autocomplete options, text inputs also support [datalists](#dat #### Telephone validation -When using a tel field the value will be validated using this default regex `/^[+]*[(]{0,1}[0-9]{1,4}[)]{0,1}[-\s\.\/0-9]*$/`. If you wish to override that you can use the `telRegex()` method. +When using a `tel()` field, the value will be validated using: `/^[+]*[(]{0,1}[0-9]{1,4}[)]{0,1}[-\s\.\/0-9]*$/`. + +If you wish to change that, that you can use the `telRegex()` method: ```php use Filament\Forms\Components\TextInput; -// Per field -TextInput::make('telephone') +TextInput::make('phone') ->tel() - ->telRegex('/^[+]*[(]{0,1}[0-9]{1,4}[)]{0,1}[-\s\.\/0-9]*$/'); + ->telRegex('/^[+]*[(]{0,1}[0-9]{1,4}[)]{0,1}[-\s\.\/0-9]*$/') +``` -// Globally -TextInput::configureUsing(function (TextInput $input): void { - $input->telRegex('/^[+]*[(]{0,1}[0-9]{1,4}[)]{0,1}[-\s\.\/0-9]*$/'); +Alternatively, to customize the `telRegex()` across all fields, use a service provider: + +```php +use Filament\Forms\Components\TextInput; + +TextInput::configureUsing(function (TextInput $component): void { + $component->telRegex('/^[+]*[(]{0,1}[0-9]{1,4}[)]{0,1}[-\s\.\/0-9]*$/'); }); ``` From 0b006f9012de376487f9cafbe5fc2f371f19934c Mon Sep 17 00:00:00 2001 From: Dan Harrin Date: Thu, 13 Oct 2022 15:35:14 +0100 Subject: [PATCH 09/11] Update 03-fields.md --- packages/forms/docs/03-fields.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/forms/docs/03-fields.md b/packages/forms/docs/03-fields.md index 2acaca3afd..577e91c83d 100644 --- a/packages/forms/docs/03-fields.md +++ b/packages/forms/docs/03-fields.md @@ -316,7 +316,7 @@ TextInput::make('password') For more complex autocomplete options, text inputs also support [datalists](#datalists). -#### Telephone validation +#### Phone number validation When using a `tel()` field, the value will be validated using: `/^[+]*[(]{0,1}[0-9]{1,4}[)]{0,1}[-\s\.\/0-9]*$/`. From adc63bc49badc9c4345494104900cf9b82b17556 Mon Sep 17 00:00:00 2001 From: Dan Harrin Date: Thu, 13 Oct 2022 15:37:00 +0100 Subject: [PATCH 10/11] Update Resource.php --- packages/admin/src/Resources/Resource.php | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/packages/admin/src/Resources/Resource.php b/packages/admin/src/Resources/Resource.php index db4de80c6c..43263f5a48 100644 --- a/packages/admin/src/Resources/Resource.php +++ b/packages/admin/src/Resources/Resource.php @@ -20,7 +20,9 @@ use Illuminate\Support\Traits\Macroable; class Resource { - use Macroable { __call as macroCall; } + use Macroable { + __call as dynamicMacroCall; + } protected static ?string $breadcrumb = null; From e9734c91b0ec006db4b39657c8e8df6aa25a2d54 Mon Sep 17 00:00:00 2001 From: oddvalue Date: Thu, 13 Oct 2022 15:47:37 +0100 Subject: [PATCH 11/11] Update 03-fields.md --- packages/forms/docs/03-fields.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/forms/docs/03-fields.md b/packages/forms/docs/03-fields.md index 577e91c83d..3d3315d2a6 100644 --- a/packages/forms/docs/03-fields.md +++ b/packages/forms/docs/03-fields.md @@ -320,7 +320,7 @@ For more complex autocomplete options, text inputs also support [datalists](#dat When using a `tel()` field, the value will be validated using: `/^[+]*[(]{0,1}[0-9]{1,4}[)]{0,1}[-\s\.\/0-9]*$/`. -If you wish to change that, that you can use the `telRegex()` method: +If you wish to change that, then you can use the `telRegex()` method: ```php use Filament\Forms\Components\TextInput;