chmielewskitomasz/validator 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

chmielewskitomasz/validator

Composer 安装命令:

composer create-project chmielewskitomasz/validator

包简介

Simple validation tool, allows you to validate both simple and nested structures, introduces conditional fields. All you need is here. Perfect for REST endpoint validation. No dependencies required.

README 文档

README

Build Status Scrutinizer Code Quality Code Coverage Build Status Latest Stable Version License

Donate on PayPal

How to install it?

composer require chmielewskitomasz/validator

How to use it?

require_once __FILE__ . '/vendor/autoload.php';

use Hop\Strategy\Strategy;
use Hop\Strategy\Field;
use Hop\StdValidator;

// first, create your input validation strategy
class AlbumInputValidator implements Strategy
{
    /**
     * @inheritdoc
     */
    public function getFields(): array
    {
        $nameField = new Field(
            'name',
            true,
            null
        );
        
        $phoneField = new Field(
            'phone'
            false,
            null
        );
        
        $phoneField->registerValidator('Digits');
        $phoneField->registerValidator('Length', ['min' => 9, 'max' => 9]);
    
        return [
            $nameField,
            $phoneField
        ];
    }
}

// second, create stdValidator
$validator = StdValidator::fromConfig(include __DIR__ . '/config/validators.php');


// three - validate your input
$myInput = [
    'name' => 'Tom',
    'phone' => '999888000'
];
$validator->isValid($myInput, new AlbumInputValidator());  // true
$validator->getMessages($myInput, new AlbumInputValidator());  // null


$myWrongInput = [
    'name' => 'Tom',
    'phone' => 'thisShouldBeA9DigitsNumber'
];
$validator->isValid($myWrongInput, new AlbumInputValidator()) // false
$validator->getMessages($myWrongInput, new AlbumInputValidato()) 
//  ['phone' => Message()]

Ok, I want my field to be optional, but if it is passed I want to validate it

//    In strategy: 
//    ...
    public function getFields(): array
    {
        $field = new Field(
            'phone',
            false, // set second parameter 'required' as false,
            null
        );
        
        $field->registerValidator('Digits', null);
        
        return [$field];
    }
//    ...


// and then
$validator->isValid([], new AlbumInputValidator()); // true
$validator->isValid(['phone' => 'abcdefg'], new AlbumInputValidator()); // false

I want a single field to be conditional. I mean, if eg street name is not passed, I don't want house number to be required.

Have a look:

//    In strategy: 
//    ...
    public function getFields(): array
    {
        $streetField = new Field(
            'street',
            false,
            null
        );
        
        $houseNumberField = new Field(
            'houseNumber',
            true,
            function (array $inputData) {
                return !isset($inputData['street']);
            }
        );
        
        return [$streetField, $houseNumberField];
    }
//    ...


// and then
$validator->isValid(['street' => 'High st.'], new AlbumInputValidator()); // false
$validator->isValid([], new AlbumInputValidator()); // true
$validator->isValid(['street' => 'High st.', 'houseNumber' => '12'], new AlbumInputValidator()); // true

How can I implement my own validators?

Easily. You can help me to contribute this project. Or if you don't want to, just create a class implementing Hop\Validator\Strategy\Strategy and pass it to StdValidator::fromConfig() as array, eg

$myValidators = [
    'MyValidator' => 'Path\To\Validator\Class'
];

$validator = StdValidator::fromConfig(
    \array_merge(include __DIR__ . '/config/validators.php', $myValidators)
);

© Chmielewski Tomasz

统计信息

  • 总下载量: 881
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 1
  • 点击次数: 0
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 1
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2018-01-28

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固