toshy/validation-bundle
最新稳定版本:2.2.0
Composer 安装命令:
composer require toshy/validation-bundle
包简介
Additional Symfony validators.
README 文档
README
Additional validators set for Symfony 6/7/8
This is a fork from secit-pl/validation-bundle.
Installation
From the command line run
$ composer require toshy/validation-bundle
Validators
NotBlankIf
This validator checks if value is not blank like a standard NotBlank Symfony validator, but also allows define the condition when the NotBlank validation should be performed using Symfony Expression Language.
From Symfony 6.2 you can also use When validator.
https://symfony.com/blog/new-in-symfony-6-2-conditional-constraints
Example usage
use SecIT\ValidationBundle\Validator\Constraints as SecITAssert; // ... #[SecITAssert\NotBlankIf("this.isSuperUser")] private ?string $email = null; public function isSuperUser(): bool { return true; }
Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
| expression | string | empty array | The expression that will be evaluated. If the expression evaluates to a false value (using ==, not ===), not blank validation won't be performed) |
| values | array | empty array | The values of the custom variables used in the expression. Values can be of any type (numeric, boolean, strings, null, etc.) |
FileExtension
This validator checks if file has valid file extension.
From Symfony 6.2 you can also use the "extensions" option in File validator.
https://symfony.com/blog/new-in-symfony-6-2-improved-file-validator
https://symfony.com/doc/6.2/reference/constraints/File.html#extensions
Example usage
use SecIT\ValidationBundle\Validator\Constraints as SecITAssert; // ... #[SecITAssert\FileExtension(["jpg", "jpeg", "png"])] private $file;
use SecIT\ValidationBundle\Validator\Constraints as SecITAssert; // ... #[SecITAssert\FileExtension(disallowedExtensions: ["jpg", "jpeg", "png"])] private $file;
Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
| validExtensions | array | empty array | Allowed/valid file extensions list |
| disallowedExtensions | array | empty array | Disallowed/invalid file extensions list |
| matchCase | bool | false | Enable/disable verifying the file extension case |
Caution! It's highly recommended to use this validator together with native Symfony File/Image validator.
use SecIT\ValidationBundle\Validator\Constraints as SecITAssert; use Symfony\Component\Validator\Constraints as Assert; // ... #[Assert\Image(maxSize: '2M', mimeTypes: ["image/jpg", "image/jpeg", "image/png"])] #[SecITAssert\FileExtension(validExtensions: ["jpg", "jpeg", "png"])] private $file;
CollectionOfUniqueElements
Checks if collection contains only unique elements.
Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
| matchCase | bool | false | Enable/disable verifying the characters case |
| customNormalizationFunction | null or callable | null | Custom normalization function |
use SecIT\ValidationBundle\Validator\Constraints as SecITAssert; // ... #[SecITAssert\CollectionOfUniqueElements()] private $collection;
This validator can also be used to validate unique files upload.
<?php declare(strict_types=1); namespace App\Form; use SecIT\ValidationBundle\Validator\Constraints\CollectionOfUniqueElements; use Symfony\Component\Form\AbstractType; use Symfony\Component\Form\Extension\Core\Type\CollectionType; use Symfony\Component\Form\Extension\Core\Type\FileType; use Symfony\Component\Form\FormBuilderInterface; class ExampleType extends AbstractType { public function buildForm(FormBuilderInterface $builder, array $options) { $builder->add('files', CollectionType::class, [ 'entry_type' => FileType::class, 'allow_add' => true, 'constraints' => [ new CollectionOfUniqueElements(), ], ]); } }
AntiXss
Checks if text contains XSS attack using voku\anti-xss library.
use SecIT\ValidationBundle\Validator\Constraints as SecITAssert; // ... #[SecITAssert\AntiXss()] private $text;
NaiveNoHtml
Perform very naive check if text contains HTML.
use SecIT\ValidationBundle\Validator\Constraints as SecITAssert; // ... #[SecITAssert\NaiveNoHtml()] private $text;
Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
| strongValidation | bool | true (recommended) | Enable/disable strong validation. Disable if you'd like to allow strings with unclosed tags such as as "I <3 You". |
BurnerEmail
Checks if email address is a throw away email addresses (burner email).
This check is perform against the list provided by wesbos/burner-email-providers.
You need to install this package manually (composer require wesbos/burner-email-providers) if you'd like to use this validator.
use SecIT\ValidationBundle\Validator\Constraints as SecITAssert; // ... #[SecITAssert\BurnerEmail()] private $email;
toshy/validation-bundle 适用场景与选型建议
toshy/validation-bundle 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 6.21k 次下载、GitHub Stars 达 0, 最近一次更新时间为 2023 年 12 月 23 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「symfony」 「file」 「validator」 「html」 「xss」 「file extension」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 toshy/validation-bundle 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 toshy/validation-bundle 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 toshy/validation-bundle 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Runn Me! Validation and Sanitization Library
Extension for Opis JSON Schema
The file manager intended for using Laravel with CKEditor / TinyMCE / Colorbox
The bundle for easy using json-rpc api on your project
Bundle Symfony DaplosBundle
Laravel Media Popup to upload/view the files
统计信息
- 总下载量: 6.21k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 23
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2023-12-23