fesor/symfony-validator-extra 问题修复 & 功能扩展

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

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

fesor/symfony-validator-extra

Composer 安装命令:

composer require fesor/symfony-validator-extra

包简介

Symfony Validator Extras for more simple handling of complex cases

README 文档

README

In order to simplify request validation and reduce amount of boilerplate, this package provides you set of additional constraints.

Json and FixedJson constraints

If you are dealing with json requests, you probably hate Collection validator. As a replacement this library provides you two additional constraints: JsonValidator and FixedJsonValidator.

The difference between JsonValidator and FixedJsonValidator, which is both extends from CollectionValidator, is that they use different value for option allowExtraFields. FixedJson doesn't allow extra fields, but Json - just ignores that.

Shortcuts

This validator provides you some shortcuts for common validators:

$rules = new FixedJson([
    'foo' => 'string',
    'bar' => 'email',
    'buz' => 'datetime',
]);

this is equivalent of

$rules = new Collection([
    'foo' => [new NotNull(), new Type('string')],
    'bar' => [new NotNull(), new Email()],
    'buz' => [new NotNull(), new DateTime()]
]);

List of available shortcuts:

ShortCut Constraint Used
date new Date()
datetime new DateTime()
time new Time()
email new Email()
url new Url()
file new File()
image new Image()
null new IsNull()
*any strhing* new Type('*any strhing*')

Null Safety

By default all constraint (except null) shortcuts expands with NotNull constraint. If null is acceptable value for you, you can just add question mark at the beginning of shortcut. So this rules will be equivalent:

$rules = new Json([
    'foo' => '?string',
    'bar' => 'string'
]);

$equivalent = new Collection([
    'allowExtraFields' => true,
    'fields' => [
        'foo' => [new Type('string')],
        'bar' => [new NotNull(), new Type('string')]
    ]
]);

The syntax is taken from PHP 7.1

Optional fields

If your json request has optional keys, then you probably will write something like this:

$rules = new Collection([
    'foo' => new Optional([new NotNull(), new Type('string')])
]);

With JsonValidator the same rule can be written as:

$rules = new Json([
    'foo?' => 'string'
]);

Please note that question mark (?) will be skipped if you manually provide info about is this field is required or not. So using this rule:

$fules = new Json([
    'foo?' = new Required([new NotNull()]),
    'bar??' => 'string',
])

validator will expect foo? containing not null value. And property bar? considered as optional since it has question mark in it's end (?).

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2016-10-18

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固