Skip to content

Commit 0c7c946

Browse files
committed
Merge branch 'release/3.0.0'
2 parents f58ec72 + 7ac0d8c commit 0c7c946

12 files changed

+320
-96
lines changed

README.md

Lines changed: 38 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,97 +1,95 @@
11
# Brazilian pt-BR form fields.
22

3-
This package provides custom form fields for [Filament](https://filamentphp.com/) (**>=v2.17.28**) that are commonly used in Brazilian web applications, such as CPF/CNPJ validation, phone number formatting, money with currency symbol, and CEP integration with [ViaCep](https://viacep.com.br).
3+
This package provides custom form fields for [Filament](https://filamentphp.com/) that are commonly used in Brazilian web applications, such as CPF/CNPJ validation, phone number formatting, money with currency symbol, and CEP integration with [ViaCep](https://viacep.com.br).
44

55
This package uses [LaravelLegends/pt-br-validator](https://github.com/LaravelLegends/pt-br-validator) to validate Brazilian Portuguese fields.
66

7-
![image demo](https://raw.githubusercontent.com/leandrocfe/filament-ptbr-form-fields/develop/screenshots/v1-example.png)
7+
![image demo](https://raw.githubusercontent.com/leandrocfe/filament-ptbr-form-fields/develop/screenshots/v3-example.png)
88

99
## Installation
1010

1111
You can install the package via Composer:
1212

1313
```bash
14-
composer require leandrocfe/filament-ptbr-form-fields
14+
composer require leandrocfe/filament-ptbr-form-fields:"^3.0"
1515
```
1616

17+
### Filament V2 - if you are using Filament v2.x, you can use [this section](https://github.com/leandrocfe/filament-ptbr-form-fields/tree/2.0.0)
18+
1719
## Usage
1820

1921
### CPF / CNPJ
2022

2123
To create a dynamic input that accepts either CPF or CNPJ, use:
2224

2325
```php
24-
use Leandrocfe\FilamentPtbrFormFields\PtbrCpfCnpj;
25-
PtbrCpfCnpj::make('cpf_or_cnpj')
26-
```
27-
28-
You can also add validation to this field by chaining the rule() method:
29-
30-
```php
31-
PtbrCpfCnpj::make('cpf_or_cnpj')
32-
->rule('cpf_ou_cnpj')
26+
use Leandrocfe\FilamentPtbrFormFields\Document;
27+
//CPF or CNPJ
28+
Document::make('cpf_or_cnpj')
29+
->dynamic()
3330
```
3431

3532
If you want to create an input that only accepts CPF or only accepts CNPJ, use:
3633

3734
```php
3835
//CPF
39-
PtbrCpfCnpj::make('cpf')
36+
Document::make('cpf')
4037
->cpf()
4138
```
4239

4340
```php
4441
//CNPJ
45-
PtbrCpfCnpj::make('cnpj')
42+
Document::make('cnpj')
4643
->cnpj()
4744
```
4845

49-
You can also add validation to these fields as well:
46+
If you want to use a custom mask for the input, use the cpf() or cnpj() method with a string argument representing the desired mask:
5047

5148
```php
52-
//CPF with validation
53-
PtbrCpfCnpj::make('cpf')
54-
->cpf()
55-
->rule('cpf')
49+
Document::make('cpf')
50+
->cpf('999999999-99')
5651
```
5752

5853
```php
59-
//CNPJ with validation
60-
PtbrCpfCnpj::make('cnpj')
61-
->cnpj()
62-
->rule('cnpj')
54+
Document::make('cnpj')
55+
->cnpj('99999999/9999-99')
6356
```
6457

65-
If you want to use a custom mask for the input, use the cpf() or cnpj() method with a string argument representing the desired mask:
58+
### Validation
59+
`Document` uses [LaravelLegends/pt-br-validator](https://github.com/LaravelLegends/pt-br-validator) to validate Brazilian Portuguese fields by default - `cpf_ou_cnpj` | `cpf` | `cnpj`
60+
61+
You can disable validation using the `validation(false)` method:
6662

6763
```php
68-
PtbrCpfCnpj::make('cpf')
69-
->cpf('999999999-99')
64+
Document::make('cpf_or_cnpj')
65+
->validation(false)
66+
->dynamic()
7067
```
7168

7269
```php
73-
PtbrCpfCnpj::make('cnpj')
74-
->cnpj('99999999/9999-57')
70+
Document::make('cpf')
71+
->validation(false)
72+
->cpf()
7573
```
7674

7775
### Phone number
7876

7977
To create a dynamic input that formats phone numbers with DDD, use:
8078

8179
```php
82-
use Leandrocfe\FilamentPtbrFormFields\PtbrPhone;
83-
PtbrPhone::make('phone_number')
80+
use Leandrocfe\FilamentPtbrFormFields\PhoneNumber;
81+
PhoneNumber::make('phone_number')
8482
```
8583

8684
If you want to use a custom phone number format, use the format() method with a string argument representing the desired format:
8785

8886
```php
89-
PtbrPhone::make('phone_number')
87+
PhoneNumber::make('phone_number')
9088
->format('99999-9999')
9189
```
9290

9391
```php
94-
PtbrPhone::make('phone_number')
92+
PhoneNumber::make('phone_number')
9593
->format('(+99)(99)99999-9999')
9694
```
9795

@@ -100,28 +98,28 @@ PtbrPhone::make('phone_number')
10098
To create a money input with the Brazilian currency symbol as the prefix, use:
10199

102100
```php
103-
use Leandrocfe\FilamentPtbrFormFields\PtbrMoney;
104-
PtbrMoney::make('price')
101+
use Leandrocfe\FilamentPtbrFormFields\Money;
102+
Money::make('price')
105103
```
106104

107105
If you want to remove the prefix, use the prefix() method with a null argument:
108106

109107
```php
110-
PtbrMoney::make('price')
108+
Money::make('price')
111109
->prefix(null)
112110
```
113111

114112
By default, the mask is removed from the input when it is submitted. If you want to keep the mask, use the dehydrateMask() method with a false argument:
115113

116114
```php
117-
PtbrMoney::make('price')
115+
Money::make('price')
118116
->dehydrateMask(false)
119117
```
120118

121119
The initial value of the input is '0,00'. If you want to change the initial value, use the initialValue() method with a string argument:
122120

123121
```php
124-
PtbrMoney::make('price')
122+
Money::make('price')
125123
->initialValue(null)
126124
```
127125

@@ -130,9 +128,9 @@ PtbrMoney::make('price')
130128
To integrate with the ViaCep API for CEP validation and address autofill, use:
131129

132130
```php
133-
use Leandrocfe\FilamentPtbrFormFields\PtbrCep;
131+
use Leandrocfe\FilamentPtbrFormFields\Cep;
134132
use Filament\Forms\Components\TextInput;
135-
PtbrCep::make('postal_code')
133+
Cep::make('postal_code')
136134
->viaCep(
137135
mode: 'suffix', // Determines whether the action should be appended to (suffix) or prepended to (prefix) the cep field, or not included at all (none).
138136
errorMessage: 'CEP inválido.', // Error message to display if the CEP is invalid.
@@ -160,7 +158,7 @@ TextInput::make('city'),
160158
TextInput::make('state'),
161159
```
162160

163-
The mode parameter specifies whether the search action should be appended to or prepended to the CEP field, using the values suffix or prefix. Alternatively, you can use the none value with the ->lazy() method to indicate that the other address fields will be automatically filled only when the CEP field loses focus.
161+
The mode parameter specifies whether the search action should be appended to or prepended to the CEP field, using the values suffix or prefix. Alternatively, you can use the none value with the `->live(onBlur: true)` method to indicate that the other address fields will be automatically filled only when the CEP field loses focus.
164162

165163
## Testing
166164

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
],
1818
"require": {
1919
"php": "^8.1",
20-
"filament/filament": "^2.17",
20+
"filament/filament": "^3.0",
2121
"illuminate/contracts": "^10.0",
2222
"laravellegends/pt-br-validator": "^10.0",
2323
"spatie/laravel-package-tools": "^1.14.0"

screenshots/v3-example.png

232 KB
Loading

src/Cep.php

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
<?php
2+
3+
namespace Leandrocfe\FilamentPtbrFormFields;
4+
5+
use Filament\Forms\Components\Actions\Action;
6+
use Filament\Forms\Components\Component;
7+
use Filament\Forms\Components\TextInput;
8+
use Filament\Forms\Set;
9+
use Illuminate\Support\Arr;
10+
use Illuminate\Support\Facades\Http;
11+
use Illuminate\Validation\ValidationException;
12+
use Livewire\Component as Livewire;
13+
14+
class Cep extends TextInput
15+
{
16+
public function viaCep(string $mode = 'suffix', string $errorMessage = 'CEP inválido.', array $setFields = []): static
17+
{
18+
$viaCepRequest = function ($state, $livewire, $set, $component, $errorMessage, array $setFields) {
19+
20+
$livewire->validateOnly($component->getKey());
21+
22+
$request = Http::get("viacep.com.br/ws/$state/json/")->json();
23+
24+
foreach ($setFields as $key => $value) {
25+
$set($key, $request[$value] ?? null);
26+
}
27+
28+
if (blank($request) || Arr::has($request, 'erro')) {
29+
throw ValidationException::withMessages([
30+
$component->getKey() => $errorMessage,
31+
]);
32+
}
33+
};
34+
35+
$this
36+
->minLength(9)
37+
->mask('99999-999')
38+
->afterStateUpdated(function ($state, Livewire $livewire, Set $set, Component $component) use ($errorMessage, $setFields, $viaCepRequest) {
39+
$viaCepRequest($state, $livewire, $set, $component, $errorMessage, $setFields);
40+
})
41+
->suffixAction(function () use ($mode, $errorMessage, $setFields, $viaCepRequest) {
42+
if ($mode === 'suffix') {
43+
return Action::make('search-action')
44+
->label('Buscar CEP')
45+
->icon('heroicon-o-magnifying-glass')
46+
->action(function ($state, Livewire $livewire, Set $set, Component $component) use ($errorMessage, $setFields, $viaCepRequest) {
47+
$viaCepRequest($state, $livewire, $set, $component, $errorMessage, $setFields);
48+
})
49+
->cancelParentActions();
50+
}
51+
})
52+
->prefixAction(function () use ($mode, $errorMessage, $setFields, $viaCepRequest) {
53+
if ($mode === 'prefix') {
54+
return Action::make('search-action')
55+
->label('Buscar CEP')
56+
->icon('heroicon-o-magnifying-glass')
57+
->action(function ($state, Livewire $livewire, Set $set, Component $component) use ($errorMessage, $setFields, $viaCepRequest) {
58+
$viaCepRequest($state, $livewire, $set, $component, $errorMessage, $setFields);
59+
})
60+
->cancelParentActions();
61+
}
62+
});
63+
64+
return $this;
65+
}
66+
}

src/Document.php

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
<?php
2+
3+
namespace Leandrocfe\FilamentPtbrFormFields;
4+
5+
use Closure;
6+
use Filament\Forms\Components\TextInput;
7+
use Filament\Support\RawJs;
8+
9+
class Document extends TextInput
10+
{
11+
public bool $validation = true;
12+
13+
public function dynamic(bool $condition = true): static
14+
{
15+
if (self::getValidation()) {
16+
$this->rule('cpf_ou_cnpj');
17+
}
18+
19+
if ($condition) {
20+
$this->mask(RawJs::make(<<<'JS'
21+
$input.length > 14 ? '99.999.999/9999-99' : '999.999.999-99'
22+
JS))->minLength(14);
23+
}
24+
25+
return $this;
26+
}
27+
28+
public function cpf(string|Closure $format = '999.999.999-99'): static
29+
{
30+
$this->dynamic(false)
31+
->mask($format);
32+
33+
if (self::getValidation()) {
34+
$this->rule('cpf');
35+
}
36+
37+
return $this;
38+
}
39+
40+
public function cnpj(string|Closure $format = '99.999.999/9999-99'): static
41+
{
42+
$this->dynamic(false)
43+
->mask($format);
44+
45+
if (self::getValidation()) {
46+
$this->rule('cnpj');
47+
}
48+
49+
return $this;
50+
}
51+
52+
public function validation(bool|Closure $condition = true): static
53+
{
54+
$this->validation = $condition;
55+
56+
return $this;
57+
}
58+
59+
public function getValidation(): bool
60+
{
61+
return $this->validation;
62+
}
63+
}

src/FilamentPtbrFormFieldsServiceProvider.php

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,6 @@
77

88
class FilamentPtbrFormFieldsServiceProvider extends FilamentServiceProvider
99
{
10-
protected array $beforeCoreScripts = [
11-
'filament-apex-charts-scripts' => __DIR__.'/../dist/mask.min.js',
12-
];
13-
1410
public function configurePackage(Package $package): void
1511
{
1612
/*

0 commit comments

Comments
 (0)