ipedis/validation-handler
最新稳定版本:2.1.0
Composer 安装命令:
composer require ipedis/validation-handler
包简介
File validation handler
README 文档
README
Validation-handler
This library is useful for file validation built on top of symfony/validator component.
Get Started
First you have to install dependency of this library.
make install
make test
How to use?
Prepare your data inside DataWrapper and create validation chain with validators.
use Ipedis\ValidationHandler\Data\DataWrapper; use Ipedis\ValidationHandler\Data\Constraints\FileSize; use Ipedis\ValidationHandler\Data\Constraints\MimeTypes; use Ipedis\ValidationHandler\Validator\Result\ValidationResult; use Ipedis\ValidationHandler\ConstraintFactory; $file = __DIR__."/../tests/data/265kb.pdf"; $data = new DataWrapper(new SplFileInfo($file)); /** * build validator with list of constraints. */ $validator = ConstraintFactory::build(constraints: [ new FileSize('100', 'k'), new MimeTypes(['application/pdf']) ]); // run validations $result = $validation->handle(); var_dump($result->isFailed(), $result->getErrorMessage()); /* * don't want to search for mimetype string? you can use built-in mimetypes helper */ // example of mimetypes helper $validator = ConstraintFactory::build(constraints: [ new FileSize('1', 'M'), MimeTypes::with(PdfMimeType::class) ]); /** @var ValidationResult $result */ $result = $validator->handle($data); // should pass. var_dump($result->isFailed(), $result->getErrorMessage()); /** * If you do not concern about filesize but only want to check if file is image or pdf */ var_dump(ValidationHelper::isImage($data)); var_dump(ValidationHelper::isPdf($data));
Result will be instanced of ValidationResult, isFailed() is helper to know
if validation failed or not. getError() will give you instance of symfony's ConstraintViolationInterface.
Adding more validator
Each validator class extends HandlerAbstract. You can create new class and perform your own required validations.
Limitations and TODO
- For now validation is only for uploaded files, we can expand it to use other types.
统计信息
- 总下载量: 27
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 4
- 依赖项目数: 1
- 推荐数: 0
其他信息
- 授权协议: Unknown
- 更新时间: 2026-03-25