You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+38-40Lines changed: 38 additions & 40 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,97 +1,95 @@
1
1
# Brazilian pt-BR form fields.
2
2
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).
4
4
5
5
This package uses [LaravelLegends/pt-br-validator](https://github.com/LaravelLegends/pt-br-validator) to validate Brazilian Portuguese fields.
### 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
+
17
19
## Usage
18
20
19
21
### CPF / CNPJ
20
22
21
23
To create a dynamic input that accepts either CPF or CNPJ, use:
22
24
23
25
```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()
33
30
```
34
31
35
32
If you want to create an input that only accepts CPF or only accepts CNPJ, use:
36
33
37
34
```php
38
35
//CPF
39
-
PtbrCpfCnpj::make('cpf')
36
+
Document::make('cpf')
40
37
->cpf()
41
38
```
42
39
43
40
```php
44
41
//CNPJ
45
-
PtbrCpfCnpj::make('cnpj')
42
+
Document::make('cnpj')
46
43
->cnpj()
47
44
```
48
45
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:
50
47
51
48
```php
52
-
//CPF with validation
53
-
PtbrCpfCnpj::make('cpf')
54
-
->cpf()
55
-
->rule('cpf')
49
+
Document::make('cpf')
50
+
->cpf('999999999-99')
56
51
```
57
52
58
53
```php
59
-
//CNPJ with validation
60
-
PtbrCpfCnpj::make('cnpj')
61
-
->cnpj()
62
-
->rule('cnpj')
54
+
Document::make('cnpj')
55
+
->cnpj('99999999/9999-99')
63
56
```
64
57
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:
66
62
67
63
```php
68
-
PtbrCpfCnpj::make('cpf')
69
-
->cpf('999999999-99')
64
+
Document::make('cpf_or_cnpj')
65
+
->validation(false)
66
+
->dynamic()
70
67
```
71
68
72
69
```php
73
-
PtbrCpfCnpj::make('cnpj')
74
-
->cnpj('99999999/9999-57')
70
+
Document::make('cpf')
71
+
->validation(false)
72
+
->cpf()
75
73
```
76
74
77
75
### Phone number
78
76
79
77
To create a dynamic input that formats phone numbers with DDD, use:
80
78
81
79
```php
82
-
use Leandrocfe\FilamentPtbrFormFields\PtbrPhone;
83
-
PtbrPhone::make('phone_number')
80
+
use Leandrocfe\FilamentPtbrFormFields\PhoneNumber;
81
+
PhoneNumber::make('phone_number')
84
82
```
85
83
86
84
If you want to use a custom phone number format, use the format() method with a string argument representing the desired format:
To create a money input with the Brazilian currency symbol as the prefix, use:
101
99
102
100
```php
103
-
use Leandrocfe\FilamentPtbrFormFields\PtbrMoney;
104
-
PtbrMoney::make('price')
101
+
use Leandrocfe\FilamentPtbrFormFields\Money;
102
+
Money::make('price')
105
103
```
106
104
107
105
If you want to remove the prefix, use the prefix() method with a null argument:
108
106
109
107
```php
110
-
PtbrMoney::make('price')
108
+
Money::make('price')
111
109
->prefix(null)
112
110
```
113
111
114
112
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:
115
113
116
114
```php
117
-
PtbrMoney::make('price')
115
+
Money::make('price')
118
116
->dehydrateMask(false)
119
117
```
120
118
121
119
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:
122
120
123
121
```php
124
-
PtbrMoney::make('price')
122
+
Money::make('price')
125
123
->initialValue(null)
126
124
```
127
125
@@ -130,9 +128,9 @@ PtbrMoney::make('price')
130
128
To integrate with the ViaCep API for CEP validation and address autofill, use:
131
129
132
130
```php
133
-
use Leandrocfe\FilamentPtbrFormFields\PtbrCep;
131
+
use Leandrocfe\FilamentPtbrFormFields\Cep;
134
132
use Filament\Forms\Components\TextInput;
135
-
PtbrCep::make('postal_code')
133
+
Cep::make('postal_code')
136
134
->viaCep(
137
135
mode: 'suffix', // Determines whether the action should be appended to (suffix) or prepended to (prefix) the cep field, or not included at all (none).
138
136
errorMessage: 'CEP inválido.', // Error message to display if the CEP is invalid.
@@ -160,7 +158,7 @@ TextInput::make('city'),
160
158
TextInput::make('state'),
161
159
```
162
160
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.
0 commit comments