定制 ipedis/validation-handler 二次开发

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

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

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

GitHub 信息

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

其他信息

  • 授权协议: Unknown
  • 更新时间: 2026-03-25

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固