定制 marcino0o/php-validator 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

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

marcino0o/php-validator

最新稳定版本:v1.2.8

Composer 安装命令:

composer require marcino0o/php-validator

包简介

Advanced validation package

README 文档

README

PHP version GitHub tag (latest by date) GitHub Release Date GitHub Coverage Status Gitlab pipeline status

PHP package for easy validation

Features

  • field sets validation
  • custom error messages

Requirements

  • PHP >= 8.1
  • composer

Installation

Using composer:

composer require "marcino0o/php-validator"

Usage

Quick example

<?php

require('vendor/autoload.php');

use Validator\FieldSetValidator;
use Validator\Field;
use Validator\Rule\Email;
use Validator\Rule\TypeString;

$dataToValidate = [
    'email' => 'joe.doe@example.com', // will pass
    'name' => null, // will pass
]; 

$validator = new FieldSetValidator(
    Field::required('email', new Email()),
    Field::optional('name', new TypeString())->nullable(),
);

if ($validator->validate($dataToValidate)->hasErrors()) {
    var_dump($validator->getErrors());
    exit;
}

// all good

Specifying allowed fields in set

<?php

require('vendor/autoload.php');

use Validator\FieldSetValidator;
use Validator\Field;
use Validator\Rule\Email;

$dataToValidate = [
    'email' => 'joe.doe@example.com', // will pass
    'name' => 'Joe Doe', // will not pass
]; 

$validator = new FieldSetValidator(
    Field::required('email', new Email()),
);
$validator->withAllowedFields('email');

Fields in sets with multidimensional arrays

<?php

require('vendor/autoload.php');

use Validator\FieldSetValidator;
use Validator\Field;
use Validator\Rule\TypeArray
use Validator\Rule\TypeString;
use Validator\Rule\Uuid;

$dataToValidate = [
    'author' => [
        'uuid' => 'c07f9405-8618-49a7-980a-e4982e307274',
        'name' => 'Joe Doe',
    ],   
]; 

$validator = new FieldSetValidator(
    Field::required('author', (new TypeArray())->withRequiredKeys('uuid', 'name')),
    Field::requiredWith('author.uuid', 'author' new Uuid()),
    Field::requiredWith('author.name', 'author' new TypeString()),
);

Field requirement options

Always required

<?php

require('vendor/autoload.php');

use Validator\Field;
use Validator\Rule\TypeString;

// example 1
Field::required('text', new TypeString())
    ->validate(['text' => 'Hello world'])
    ->hasErrors(); // will return false

// example 2
Field::required('text', new TypeString())
    ->validate([])
    ->hasErrors(); // will return true

Optional

<?php

require('vendor/autoload.php');

use Validator\Field;
use Validator\Rule\TypeString;

// example 1
Field::optional('text', new TypeString())
    ->validate(['text' => 'Hello world'])
    ->hasErrors(); // will return false

// example 2
Field::optional('text', new TypeString())
    ->validate([])
    ->hasErrors(); // will return false

Required with other field

<?php

require('vendor/autoload.php');

use Validator\Field;
use Validator\Rule\TypeString;

// example 1
Field::requiredWith('i_will_be_required', 'when_i_exists', new TypeString())
    ->validate(['when_i_exists' => 'Hello world'])
    ->hasErrors(); // will return true

// example 2
Field::requiredWith('i_will_be_required', 'when_i_exists', new TypeString())
    ->validate([
        'i_will_be_required' => 'Hello universe',
        'when_i_exists' => 'Hello world',
    ])
    ->hasErrors(); // will return false

// example 3
Field::requiredWith('i_will_be_required', 'when_i_exists', new TypeString())
    ->validate(['not_related_param' => 'Hello world'])
    ->hasErrors(); // will return false

Custom messages

<?php

require('vendor/autoload.php');

use Validator\Dictionary\TypeStringDictionary as Dictionary;
use Validator\Rule\TypeString;

$example1 = new TypeString;
$example1->withMessages([
    Dictionary::LENGTH_TOO_SHORT => 'C\'mon, {{ minLength }} characters it\'s not much! You can write more than "{{ value }}"',
    Dictionary::LENGTH_TOO_LONG => 'Take it easy! There is a space for only {{ maxLength }} characters!',
]);

$example1->lengthBetween(5, 1000)->isSatisfiedBy('abc'); // false
$example1->getErrors()->first()->getMessage(); // C'mon, 5 characters it's not much! You can write more than "abc"


$example2 = new TypeString;
$example1->withMessage(Dictionary::LENGTH_TOO_SHORT, 'Text is too short.')

Available rules

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2023-03-15