Skip to content

Commit e405672

Browse files
authored
Update README.md
1 parent e7da076 commit e405672

File tree

1 file changed

+49
-1
lines changed

1 file changed

+49
-1
lines changed

README.md

Lines changed: 49 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# PHP validate
1+
# PHP Validate
22

33
[![License](https://img.shields.io/packagist/l/inhere/php-validate.svg?style=flat-square)](LICENSE)
44
[![Php Version](https://img.shields.io/badge/php-%3E=7.1-brightgreen.svg?maxAge=2592000)](https://packagist.org/packages/inhere/php-validate)
@@ -591,6 +591,54 @@ $v->validate();
591591
- 通过类 `Filtration`,可以独立使用过滤器功能
592592
- php内置过滤器请参看 http://php.net/manual/zh/filter.filters.sanitize.php
593593

594+
## 场景验证
595+
596+
如果需要让定义的规则在多个类似情形下重复使用,可以设置规则的使用场景。 **在验证时也表明要验证的场景**
597+
598+
### 方式1
599+
600+
在继承类中使用 `scenarios()` 方法:
601+
602+
```php
603+
// 在继承了 Validation 的子类 ValidationClass 中 ...
604+
605+
// 定义不同场景需要验证的字段。
606+
// 功能跟规则里的 'on' 类似,两者尽量不要同时使用,以免混淆。
607+
public function scenarios(): array
608+
{
609+
return [
610+
'create' => ['user', 'pwd', 'code'],
611+
'update' => ['user', 'pwd'],
612+
];
613+
}
614+
```
615+
616+
### 方式2
617+
618+
添加规则时配置 `on` 选项:
619+
620+
```php
621+
// 在继承了 Validation 的子类 ValidationClass 中 ...
622+
public function rules(): array
623+
{
624+
return [
625+
['title', 'required' ],
626+
['userId', 'number', 'on' => 'create' ],
627+
['userId', 'int', 'on' => 'update' ],
628+
['name', 'string', 'on' => 'create,update' ],
629+
];
630+
}
631+
```
632+
633+
### 验证使用
634+
635+
```php
636+
637+
$v->setSecne('update')->validate();
638+
639+
```
640+
641+
594642
<a name="built-in-filters"></a>
595643
## 内置的过滤器
596644

0 commit comments

Comments
 (0)