Skip to content

Commit fd53104

Browse files
authored
Merge pull request #8231 from dereuromark/docs-validation-providers
Docs: Improve validation providers section
2 parents 6575fc4 + 0ea5dae commit fd53104

File tree

1 file changed

+24
-1
lines changed

1 file changed

+24
-1
lines changed

docs/en/orm/validation.md

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,29 @@ class UsersTable extends Table
235235
}
236236
```
237237

238+
### Using Provider Methods Directly
239+
240+
In some cases, you may want to call provider methods directly within a custom
241+
validation rule. You can access a provider class using `getProvider()` and then
242+
call its methods statically. However, for the default `Validation` class, you
243+
can simply call its methods directly:
244+
245+
```php
246+
use Cake\Validation\Validation;
247+
248+
$validator->add('start_on', 'dateOrDatetime', [
249+
'rule' => function ($value): bool {
250+
return Validation::datetime($value) || Validation::date($value);
251+
},
252+
'message' => __('Must be a date or datetime'),
253+
]);
254+
```
255+
256+
This approach is useful when you need to combine multiple validation rules with
257+
OR logic, which is not directly supported by the validator's fluent interface.
258+
259+
## Using Closures as Validation Rules
260+
238261
You can also use closures for validation rules:
239262

240263
```php
@@ -245,7 +268,7 @@ $validator->add('name', 'myRule', [
245268
}
246269

247270
return 'Not a good value.';
248-
}
271+
},
249272
]);
250273
```
251274

0 commit comments

Comments
 (0)