toshy / validation-bundle
额外的 Symfony 验证器。
Requires
- php: >=8.0.0
- symfony/dependency-injection: ^6.0|^7.0
- symfony/expression-language: ^6.0|^7.0
- symfony/framework-bundle: ^6.0|^7.0
- symfony/http-foundation: ^6.0|^7.0
- symfony/validator: ^6.0|^7.0
- voku/anti-xss: ^4.1
Requires (Dev)
- doctrine/orm: ^2.6
- phpunit/phpunit: @stable
Suggests
- wesbos/burner-email-providers: Needed for BurnerEmailValidator
This package is auto-updated.
Last update: 2024-08-23 19:44:40 UTC
README
为 Symfony 6/7 设置的额外验证器。
这是一个从 secit-pl/validation-bundle 分支出来的。
安装
在命令行运行
$ composer require toshy/validation-bundle
验证器
NotBlankIf
此验证器检查值是否不为空,就像标准的 NotBlank Symfony 验证器一样,但还允许使用 Symfony 表达式语言定义 NotBlank 验证应该执行的条件。
从 Symfony 6.2 开始,您还可以使用 When 验证器。
https://symfony.ac.cn/blog/new-in-symfony-6-2-conditional-constraints
https://symfony.ac.cn/doc/6.2/reference/constraints/When.html
示例用法
use SecIT\ValidationBundle\Validator\Constraints as SecITAssert; // ... #[SecITAssert\NotBlankIf("this.isSuperUser")] private ?string $email = null; public function isSuperUser(): bool { return true; }
参数
FileExtension
此验证器检查文件是否有有效的文件扩展名。
从 Symfony 6.2 开始,您还可以在 File 验证器中使用 "extensions" 选项。
https://symfony.ac.cn/blog/new-in-symfony-6-2-improved-file-validator
https://symfony.ac.cn/doc/6.2/reference/constraints/File.html#extensions
示例用法
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;
参数
注意!强烈建议您与原生 Symfony 文件/图像验证器一起使用此验证器。
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
检查集合是否只包含唯一元素。
参数
use SecIT\ValidationBundle\Validator\Constraints as SecITAssert; // ... #[SecITAssert\CollectionOfUniqueElements()] private $collection;
此验证器也可以用来验证唯一文件上传。
<?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
检查文本是否使用 voku\anti-xss 库包含 XSS 攻击。
use SecIT\ValidationBundle\Validator\Constraints as SecITAssert; // ... #[SecITAssert\AntiXss()] private $text;
NaiveNoHtml
对文本是否包含 HTML 执行非常简单的检查。
use SecIT\ValidationBundle\Validator\Constraints as SecITAssert; // ... #[SecITAssert\NaiveNoHtml()] private $text;
参数
BurnerEmail
检查电子邮件地址是否是一次性电子邮件地址(burner email)。此检查针对 wesbos/burner-email-providers 提供的列表执行。如果您想使用此验证器,需要手动安装此包(composer require wesbos/burner-email-providers
)。
use SecIT\ValidationBundle\Validator\Constraints as SecITAssert; // ... #[SecITAssert\BurnerEmail()] private $email;