Skip to content

Commit a24e93f

Browse files
authored
Added docs for validated-dto (#840)
1 parent 77a2c8a commit a24e93f

4 files changed

Lines changed: 92 additions & 0 deletions

File tree

en/components/validated-dto.md

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -326,6 +326,29 @@ protected function attributes() {
326326
}
327327
```
328328

329+
## Customizing Validation Rules
330+
331+
You can add DTO-specific custom rules in the `afterValidatorResolving` method, or add global custom rules through Hyperf's `ValidatorFactoryResolved` event.
332+
333+
This depends on your code maintenance habits. If the rules are only used in a single DTO, it is recommended to add them in the `afterValidatorResolving` method. According to the `principle of proximity`, this will make the source code easier to read.
334+
335+
```php
336+
337+
use Hyperf\Contract\ValidatorInterface;
338+
use Hyperf\Validation\Validator;
339+
340+
protected function afterValidatorResolving(ValidatorInterface $validator): void
341+
{
342+
if (! $validator instanceof Validator) {
343+
return;
344+
}
345+
346+
$validator->addExtension('diy_rule', function ($attribute, $value, $parameters, $validator) {
347+
return $value === 'diy_rule';
348+
});
349+
}
350+
```
351+
329352
## Type Casting
330353

331354
You can easily cast your DTO properties by defining the `casts` method in your DTO:

zh-cn/components/validated-dto.md

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -326,6 +326,29 @@ protected function attributes() {
326326
}
327327
```
328328

329+
## 自定义规则
330+
331+
你可以在 `afterValidatorResolving` 方法中添加 DTO 专用的自定义规则,也可以通过 Hyperf 的 `ValidatorFactoryResolved` 事件添加全局自定义规则。
332+
333+
这取决于你的代码维护习惯,如果规则仅在单一 DTO 中使用,那么建议在 `afterValidatorResolving` 方法中添加,根据 `就近原则` 将会使得源码更易读。
334+
335+
```php
336+
337+
use Hyperf\Contract\ValidatorInterface;
338+
use Hyperf\Validation\Validator;
339+
340+
protected function afterValidatorResolving(ValidatorInterface $validator): void
341+
{
342+
if (! $validator instanceof Validator) {
343+
return;
344+
}
345+
346+
$validator->addExtension('diy_rule', function ($attribute, $value, $parameters, $validator) {
347+
return $value === 'diy_rule';
348+
});
349+
}
350+
```
351+
329352
## 类型转换
330353

331354
你可以通过在 `DTO` 中定义 `casts` 方法轻松转换你的 DTO 属性:

zh-hk/components/validated-dto.md

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -326,6 +326,29 @@ protected function attributes() {
326326
}
327327
```
328328

329+
## 自定義規則
330+
331+
你可以在 `afterValidatorResolving` 方法中添加 DTO 專用的自定義規則,也可以通過 Hyperf 的 `ValidatorFactoryResolved` 事件添加全局自定義規則。
332+
333+
這取決於你的代碼維護習慣,如果規則僅在單一 DTO 中使用,那麼建議在 `afterValidatorResolving` 方法中添加,根據 `就近原則` 將會使得源碼更易讀。
334+
335+
```php
336+
337+
use Hyperf\Contract\ValidatorInterface;
338+
use Hyperf\Validation\Validator;
339+
340+
protected function afterValidatorResolving(ValidatorInterface $validator): void
341+
{
342+
if (! $validator instanceof Validator) {
343+
return;
344+
}
345+
346+
$validator->addExtension('diy_rule', function ($attribute, $value, $parameters, $validator) {
347+
return $value === 'diy_rule';
348+
});
349+
}
350+
```
351+
329352
## 類型轉換
330353

331354
你可以通過在 `DTO` 中定義 `casts` 方法輕鬆轉換你的 DTO 屬性:

zh-tw/components/validated-dto.md

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -326,6 +326,29 @@ protected function attributes() {
326326
}
327327
```
328328

329+
## 自定義規則
330+
331+
你可以在 `afterValidatorResolving` 方法中新增 DTO 專用的自定義規則,也可以透過 Hyperf 的 `ValidatorFactoryResolved` 事件新增全域性自定義規則。
332+
333+
這取決於你的程式碼維護習慣,如果規則僅在單一 DTO 中使用,那麼建議在 `afterValidatorResolving` 方法中新增,根據 `就近原則` 將會使得原始碼更易讀。
334+
335+
```php
336+
337+
use Hyperf\Contract\ValidatorInterface;
338+
use Hyperf\Validation\Validator;
339+
340+
protected function afterValidatorResolving(ValidatorInterface $validator): void
341+
{
342+
if (! $validator instanceof Validator) {
343+
return;
344+
}
345+
346+
$validator->addExtension('diy_rule', function ($attribute, $value, $parameters, $validator) {
347+
return $value === 'diy_rule';
348+
});
349+
}
350+
```
351+
329352
## 型別轉換
330353

331354
你可以透過在 `DTO` 中定義 `casts` 方法輕鬆轉換你的 DTO 屬性:

0 commit comments

Comments
 (0)