hnrazevedo/validator
Composer 安装命令:
composer require hnrazevedo/validator
包简介
Validator is a simple data validation component.
README 文档
README
The Validator is a simple data validation component. It can be used statically with a controller or together as middleware. Its author is not a professional in the development area, just someone in the Technology area who is improving his knowledge.
O Validator é um simples componente de validação de dados. Ele pode ser usado de forma estática com algum controlador ou em conjunto como middleware. Seu autor não é profissional da área de desenvolvimento, apenas alguem da área de Tecnologia que está aperfeiçoando seus conhecimentos.
Highlights
- Easy to set up (Fácil de configurar)
- Current validation rules (Regras de validação atuais)
- Follows standard PSR-15 (Segue padrão o PSR-15)
- Composer ready (Pronto para o composer)
Installation
Validator is available via Composer:
"hnrazevedo/validator": "^2.1"
or in at terminal
composer require hnrazevedo/validator
Documentation
For details on how to use the Validator, see the sample folder with details in the component directory
Para mais detalhes sobre como usar o Validator, veja a pasta de exemplos com detalhes no diretório do componente
Validation rules
Available rules
- minlength: integer
- maxlength: integer
- required: boolean
- equals: string
- type: string
- mincount: integer - For arrays values
- maxcount: integer - For arrays values
- filter: integer - See https://www.php.net/manual/en/filter.filters.validate.php for more details of the available filters
- regex: string
Languages
The system has been configured to support multiple languages, so make sure the desired language is in the languages folder and define it with the lang method before any procedure
O sistema foi configurado para suportar vários idiomas, então verifique se o idioma desejado se encontra na pasta languages e defina-o com o método lang antes de qualquer procedimento
$valid = Validator::lang('pt_br')->namespace('App\\Rules')->execute($data);
NOTE
In case the field is an array, the rules will be tested in all its elements.
Em caso do campo ser um array, as regras serão testadas em todos seus elementos.
The validation rules must be defined in the construction of the class object with the namespace denified in the Validator
As regras de validação devem ser definidas na construção do objeto da classe com o namespace denifida no Validator
namespace App\Rules; use HnrAzevedo\Validator\Validator; use HnrAzevedo\Validator\Rules; Class User{ public function __construct() { Validator::add($this, function(Rules $rules){ $rules->action('login') /* * @property string $inputName * @property array $rules * @property string $textPlaceholder */ ->field('email',['minlength'=>1,'filter'=>FILTER_VALIDATE_EMAIL,'required'=>true],'Email address') ->field('password',['minlength'=>6,'maxlength'=>20,'required'=>true],'Password') ->field('password2',['equals'=>'password','required'=>true],'Confirm password') ->field('remember',['minlength'=>2,'maxlength'=>2,'required'=>false]) ->field('birth',['type'=>'date','required'=>true],'Date of birth') ->field('phones',['mincount'=>2,'maxcount'=>3,'required'=>true,'minlength'=>8,'maxlength'=>9]); return $rules; }); return $this; } }
Data format for validation
The data for validation must be passed to the component as follows
Os dados para validação devem ser passados para o componente da seguinte forma
$data = [ 'email'=> 'hnr.azevedo@gmail.com', 'password' => 123456, 'password2' => 123456, 'phones' => [ '949164770','949164771','949164772' ], 'birth' => '28/09/1996' '_PROVIDER' => 'user', /* Class responsible for validations */ '_ROLE' => 'login' /* Form action */ ];
Defining namespace
To use dynamically to avoid the need to place the entire namespace of a class with rules, you must define the namespace before performing data validation
Para utilização de forma dinamica para evitar a necessidade de colocar o namespace inteiro de uma class com regras, deve-se definir o namespace antes de executar a validação dos dados
Check data
Validation errors are returned in an error array, in case there are more than one occurrence, they can be displayed at the same time
Os erros de validação são retornados em uma matriz de erro, caso haja mais de uma ocorrência, eles podem ser exibidos ao mesmo tempo
$valid = Validator::namespace('App\\Rules')->execute($data); if(!$valid){ $errors = []; foreach(Validator::getErrors() as $err => $message){ $errors[] = [ 'input' => array_keys($message)[0], // Return name input error 'message' => $message[array_keys($message)[0]] // Return message error ]; } }
toJson
Returns a readable Json for validation to be performed on the client side
Retorna um Json legível para validação a ser realizada no lado do cliente
$json = Validator::namespace('App\\Rules')->toJson($data); /** * Result: * { * email:{required:true,filter:274,minlength:1}, * password:{required:true,maxlength:20,minlength:6}, * password2:{required:true,equals:"password"}, * remember:{required:false,maxlength:2,minlength:2}, * birth:{required:true,type:"date"}, * phones:{maxlength:9,minlength:8,required:true,maxcount:3,mincount:2} * } */
Middleware
To use HnrAzevedo\Validator as middleware, it is necessary to define the following information in ServerRequest
Para utilizar o HnrAzevedo\Validator como middleware, é necessário definir as seguintes informações na ServerRequest
/* * @property string $namespace * @property array $data * @property string $language */ $serverRequest = $serverRequest->withAttribute('validator',[ 'namespace' => 'HnrAzevedo\\Validator\\Example\\Rules', 'data' => $data, 'lang' => 'pt_br' ]);
Support
Security: If you discover any security related issues, please email hnr.azevedo@gmail.com instead of using the issue tracker.
Se você descobrir algum problema relacionado à segurança, envie um e-mail para hnr.azevedo@gmail.com em vez de usar o rastreador de problemas.
Credits
- Henri Azevedo (Developer)
License
The MIT License (MIT). Please see License File for more information.
hnrazevedo/validator 适用场景与选型建议
hnrazevedo/validator 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 384 次下载、GitHub Stars 达 0, 最近一次更新时间为 2020 年 08 月 05 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 hnrazevedo/validator 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 hnrazevedo/validator 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 384
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 0
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2020-08-05