alanfabiano/util-validation

Laravel自定义验证

此包的官方仓库似乎已不存在,因此该包已被冻结。

v1.0.7 2017-12-19 14:02 UTC

This package is not auto-updated.

Last update: 2023-03-04 11:04:51 UTC


README

扩展Laravel 5表单字段的验证包

安装

首先,您需要使用Composer安装此包

composer require alanfabiano/util-validation

然后,从命令行运行composer update

然后,更新config/app.php文件,添加服务提供者的条目。

'providers' => [
    
    ...

    'AlanMilani\UtilValidation\UtilValidationServiceProvider::class',
];

进入文件validation.php,按照以下代码输入消息参数

return [

    ...

    /*
    |--------------------------------------------------------------------------
    | Custom Validation of package Util Validate
    |--------------------------------------------------------------------------
    */
    
    'phone'       => 'The field :attribute does not contain a valid phone number',
    'cpf'         => 'The field :attribute does not contain a valid number',
    'cep'         => 'The field :attribute does not contain a valid zip code',
    'credit_card' => 'The field :attribute does not contain a valid credit card',
    'cnpj'        => 'The field :attribute does not contain a valid CNPJ',
    'username'    => 'The field :attribute do not a valid username',

];

使用方法

public function rules()
{
    return [
        // VALID FORMAT: "(99) 99999 9999", "(99) 9999 9999"
        'field_phone' => 'phone',

        // VALID FORMAT: "999.999.999-99"
        'field_cpf'   => 'cpf',

        // VALID FORMAT: "99999-999", "99999999"
        'field_cep'   => 'cep',

        // VALID FORMAT: "99999-999", "99999999", validate using custom API. Request exemple: https://api.pagar.me/1/zipcodes/zipcode/93700000
        'field_cep'   => 'cep:https://api.pagar.me/1/zipcodes/zipcode/',

        // VALID FORMAT: "1234.1234.1234.1234", "1234-1234-1234-1234", "1234123412341234"
        'field_credit_card' => 'credit_card'

        // VALID FORMAT: "11.444.777/0001-61", "11444777000161"
        'field_cnpj' => 'cnpj'

        // VALID FORMAT: "username123", "user-name123", "user_name123", "user.name123"
        'field_username' => 'username'
    ];
}

版权与许可

Util-Validate由Alan Milani于2016年7月编写。

  • 更新于2017年12月