Skip to content

Commit be8856c

Browse files
authored
Merge pull request #26 from nonamephp/feature/type-inheritance
Register extendable validator types
2 parents 6dfd0f1 + 12e6908 commit be8856c

File tree

3 files changed

+369
-225
lines changed

3 files changed

+369
-225
lines changed

README.md

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -119,8 +119,7 @@ if(!$validator->validate()){
119119
* `alnum`, `alphanumeric` Validate that value is alpha-numeric only
120120
* `alpha` Validate that value is alpha only
121121
* `arr`, `array` Validate that value is array
122-
* `obj`, `object` Validate that value is object
123-
* `closure` Validate that value is instance of `\Closure`
122+
* `object` Validate that value is object
124123
* `callable` Validate that value is callable
125124
* `email` Validate that value is valid email address
126125
* `ip` Validate that value is either of IPv4 or IPv6
@@ -136,6 +135,31 @@ if(!$validator->validate()){
136135

137136
Create instance of `Validator`.
138137

138+
##### `addType(string $typeName, array $typeRule)`
139+
140+
Add custom validator type.
141+
142+
```php
143+
<?php
144+
use Noname\Common\Validator;
145+
146+
$values = ['a' => 2];
147+
$rules = ['a' => ['type' => 'equals_2']];
148+
$validator = new Validator();
149+
$validator->addType('equals_2', [
150+
'extends' => 'numeric',
151+
'validator' => function ($value, $rule, $validator) {
152+
$valid = $value === 2;
153+
if (!$valid) {
154+
$validator->setError($rule['name'], 'Value does not equal 2');
155+
}
156+
return $valid;
157+
}
158+
]);
159+
$validator->validate();
160+
```
161+
162+
139163
##### `addValue(string $name, mixed $value)`
140164

141165
Add value.

0 commit comments

Comments
 (0)