yodaylay22/filament-ptbr-form-fields

巴西 pt-BR 表单字段。

3.0.9 2024-08-13 08:20 UTC

README

本软件包为 Filament(Filament)提供自定义表单字段,这些字段在巴西网络应用程序中常用,例如 CPF/CNPJ 验证、电话号码格式化、带货币符号的货币以及与 ViaCep 的 CEP 集成。

本软件包使用 LaravelLegends/pt-br-validator 来验证巴西葡萄牙字段。

image demo

安装

您可以通过 Composer 安装此软件包

composer require leandrocfe/filament-ptbr-form-fields:"^3.0"

Filament V2 - 如果您正在使用 Filament v2.x,您可以使用 此部分

用法

CPF / CNPJ

要创建一个动态输入,可以接受 CPF 或 CNPJ,请使用

use Leandrocfe\FilamentPtbrFormFields\Document;
//CPF or CNPJ
Document::make('cpf_or_cnpj')
    ->dynamic()

如果您想创建一个只能接受 CPF 或只能接受 CNPJ 的输入,请使用

//CPF
Document::make('cpf')
    ->cpf()
//CNPJ
Document::make('cnpj')
    ->cnpj()

如果您想为输入使用自定义掩码,请使用带有代表所需掩码的字符串参数的 cpf() 或 cnpj() 方法

Document::make('cpf')
    ->cpf('999999999-99')
Document::make('cnpj')
    ->cnpj('99999999/9999-99')

验证

Document 默认使用 LaravelLegends/pt-br-validator 来验证巴西葡萄牙字段 - cpf_ou_cnpj | cpf | cnpj

您可以使用 validation(false) 方法禁用验证

Document::make('cpf_or_cnpj')
    ->validation(false)
    ->dynamic()
Document::make('cpf')
    ->validation(false)
    ->cpf()

电话号码

要创建一个动态输入,可以格式化带区号的电话号码,请使用

use Leandrocfe\FilamentPtbrFormFields\PhoneNumber;
PhoneNumber::make('phone_number')

如果您想使用自定义的电话号码格式,请使用带有代表所需格式的字符串参数的 format() 方法

PhoneNumber::make('phone_number')
->format('99999-9999')
PhoneNumber::make('phone_number')
->format('(+99)(99)99999-9999')

货币

要创建一个带有巴西货币符号作为前缀的货币输入,请使用

use Leandrocfe\FilamentPtbrFormFields\Money;
Money::make('price')

如果您想删除前缀,请使用带有 null 参数的 prefix() 方法

Money::make('price')
->prefix(null)

默认情况下,当输入被提交时,掩码将从不输入中删除。如果您想保留掩码,请使用带有 false 参数的 dehydrateMask() 方法

Money::make('price')
->dehydrateMask(false)

输入的初始值是 '0,00'。如果您想更改初始值,请使用带有字符串参数的 initialValue() 方法

Money::make('price')
->initialValue(null)

地址

要集成 ViaCep API 进行 CEP 验证和地址自动填充,请使用

use Leandrocfe\FilamentPtbrFormFields\Cep;
use Filament\Forms\Components\TextInput;
Cep::make('postal_code')
    ->viaCep(
        mode: 'suffix', // Determines whether the action should be appended to (suffix) or prepended to (prefix) the cep field, or not included at all (none).
        errorMessage: 'CEP inválido.', // Error message to display if the CEP is invalid.

        /**
         * Other form fields that can be filled by ViaCep.
         * The key is the name of the Filament input, and the value is the ViaCep attribute that corresponds to it.
         * More information: https://viacep.com.br/
         */
        setFields: [
            'street' => 'logradouro',
            'number' => 'numero',
            'complement' => 'complemento',
            'district' => 'bairro',
            'city' => 'localidade',
            'state' => 'uf'
        ]
    ),

TextInput::make('street'),
TextInput::make('number'),
TextInput::make('complement'),
TextInput::make('district'),
TextInput::make('city'),
TextInput::make('state'),

模式参数指定搜索操作是追加到还是前置到 CEP 字段,使用 suffix 或 prefix 的值。或者,您可以使用 none 值与 ->live(onBlur: true) 方法一起使用,以指示其他地址字段仅在 CEP 字段失去焦点时自动填充。

测试

composer test

变更日志

有关最近更改的更多信息,请参阅 CHANGELOG

贡献

有关详细信息,请参阅 CONTRIBUTING

安全漏洞

如果您在此软件包中发现安全漏洞,请通过电子邮件发送到 leandrocfe@gmail.com

鸣谢

许可

麻省理工学院许可证(MIT)。请参阅许可证文件获取更多信息。