首页 > 编程语言 >PHP轻量级验证器 Particle\Validator

PHP轻量级验证器 Particle\Validator

时间:2022-11-24 16:23:01浏览次数:62  
标签:last name Particle Validator user PHP 轻量级 result

Particle\Validator是一个小巧优雅的实用的PHP验证类库,提供了一个非常简洁的API。它无需依赖其他组件,提供友好的文档,并且有利于扩展。

composer require particle/validator
use Particle\Validator\Validator;

$v = new Validator;

$v->required('user.first_name')->lengthBetween(2, 50)->alpha();
$v->required('user.last_name')->lengthBetween(2, 50)->alpha();
$v->required('newsletter')->bool();

$result = $v->validate([
    'user' => [
        'first_name' => 'John',
        'last_name' => 'D',
    ],
    'newsletter' => true,
]);

$result->isValid(); // bool(false).
$result->getMessages();
/**
 * array(1) {
 *     ["user.last_name"]=> array(1) {
 *         ["Length::TOO_SHORT"]=> string(53) "last_name is too short and must be 2 characters long."
 *     }
 * }
 */

 

标签:last,name,Particle,Validator,user,PHP,轻量级,result
From: https://www.cnblogs.com/felixwan/p/16922251.html

相关文章