PHPackages                             xu767142206/x-validated - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Validation &amp; Sanitization](/categories/validation)
4. /
5. xu767142206/x-validated

ActiveLibrary[Validation &amp; Sanitization](/categories/validation)

xu767142206/x-validated
=======================

Request Data Validated For Hyperf

v2.0.0.x-dev(3y ago)3394↓100%MITPHPPHP &gt;=8.0

Since Oct 20Pushed 3y ago1 watchersCompare

[ Source](https://github.com/xu767142206/x-validated)[ Packagist](https://packagist.org/packages/xu767142206/x-validated)[ RSS](/packages/xu767142206-x-validated/feed)WikiDiscussions main Synced 1mo ago

READMEChangelogDependencies (1)Versions (4)Used By (0)

x-validated
===========

[](#x-validated)

简易版注解验证库 for Hyperf， 基于 hyperf/validation

> 起因：一直眼馋springboot的validation库,正好php8也有了原生的注解,就想到了套娃的方式简易实现 后面有闲情了再想办法自己实现吧

安装
--

[](#安装)

```
composer require xu767142206/x-validated
```

配置
--

[](#配置)

1.config/autoload/exceptions.php 也可以自己实现ValidationException的处理

```
return [
    'handler' => [
        'http' => [
            //....
            Hyperf\Validation\ValidationExceptionHandler::class,
            //....
        ],
    ],
];
```

使用
--

[](#使用)

dto模型的定义

> dto模型必须继承 XValidated\\ValidatedData

模型的定义

```
use XValidated\Annotation\Rule;
use XValidated\ValidatedData;

class LoginDto extends ValidatedData
{
    #[Rule(values: ["required"])]
    public int $phone;

    #[Rule(values: ["required"], alias: "pass_word", messages: ['required' => "密码不能为空"])]
    public string $password;
}
```

> rule规则可以查看 hyperf/validation库的验证规则 [验证规则](https://hyperf.wiki/3.0/#/zh-cn/validation?id=%e9%aa%8c%e8%af%81%e8%a7%84%e5%88%99)

然后创建控制器 `IndexController`：

```
use XValidated\Annotation\Validated;
use App\Model\LoginDto;
use Hyperf\HttpServer\Annotation\AutoController;
use Hyperf\HttpServer\Annotation\RequestMapping;
use Hyperf\HttpServer\Contract\RequestInterface;

#[AutoController]
class IndexController extends AbstractController
{
    #[RequestMapping(path: "index", methods: "get,post")]
    #[Validated]
    public function index(RequestInterface $request, LoginDto $loginDto)
    {
        var_dump($loginDto->toArray());
        return [
            'param' => $request->getServerParams(),
        ];
    }
}
```

接着可以快乐的写代码了

License
-------

[](#license)

MIT

###  Health Score

23

—

LowBetter than 27% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity18

Limited adoption so far

Community4

Small or concentrated contributor base

Maturity39

Early-stage or recently created project

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~1 days

Total

3

Last Release

1297d ago

Major Versions

v1.0.0.x-dev → v2.0.0-rc2022-10-21

PHP version history (2 changes)v1.0.0.x-devPHP &gt;=7.4

v2.0.0-rcPHP &gt;=8.0

### Community

Maintainers

![](https://www.gravatar.com/avatar/b4bb1bf028ef6c5ccd7b9a3bb879231f2555901f95ad998005bcc21bd6c80c86?d=identicon)[xu767142206](/maintainers/xu767142206)

---

Tags

phphyperfvalidated

### Embed Badge

![Health badge](/badges/xu767142206-x-validated/health.svg)

```
[![Health](https://phpackages.com/badges/xu767142206-x-validated/health.svg)](https://phpackages.com/packages/xu767142206-x-validated)
```

###  Alternatives

[friendsofhyperf/validated-dto

The Data Transfer Objects with validation for Hyperf.

1412.9k](/packages/friendsofhyperf-validated-dto)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
