mirror of
https://github.com/Respect/Validation.git
synced 2026-03-14 22:35:45 +01:00
The "protected" isValid() method didn't work, threw an exception sying it needs to be "public". Also the registration didn't work that way. I was unable to fix it but found an alternate way which works. Maybe it's not the right one but I'm indicating it here.
56 lines
1.7 KiB
Markdown
56 lines
1.7 KiB
Markdown
<!--
|
|
SPDX-License-Identifier: MIT
|
|
SPDX-FileCopyrightText: (c) Respect Project Contributors
|
|
SPDX-FileContributor: Alexandre Gomes Gaigalas <alganet@gmail.com>
|
|
SPDX-FileContributor: Henrique Moody <henriquemoody@gmail.com>
|
|
-->
|
|
|
|
# Custom validators
|
|
|
|
You can also create and use your own validators. To do this, you will need to create
|
|
a validator and an exception to go with the validator.
|
|
|
|
To create a validator, you need to create a class that implements the `Validator` interface
|
|
and is within the Validators `namespace`. It is convenient to just extend the `Simple` or
|
|
`Standard` class. When the validator is called the logic inside the validate method will be
|
|
executed. Here's how the class should look:
|
|
|
|
```php
|
|
namespace My\Validation\Validators;
|
|
|
|
use Respect\Validation\Message\Template;
|
|
use Respect\Validation\Validators\Core\Simple;
|
|
|
|
#[Template(
|
|
'{{subject}} is something',
|
|
'{{subject}} is not something',
|
|
)]
|
|
final class Something extends Simple
|
|
{
|
|
public function isValid(mixed $input): bool
|
|
{
|
|
// Do something here with the $input and return a boolean value
|
|
}
|
|
}
|
|
```
|
|
|
|
The `'{{subject}} is not something` message would be used when you call the validator
|
|
with the `not()`.
|
|
|
|
All classes in Validation are created by the `Factory` class. If you want
|
|
Validation to execute your validator (or validators) in the chain, you must overwrite the
|
|
default `Factory`.
|
|
|
|
```php
|
|
use Respect\Validation\ContainerRegistry;
|
|
|
|
ContainerRegistry::setContainer(
|
|
ContainerRegistry::createContainer([
|
|
'respect.validation.rule_factory.namespaces' => [
|
|
'My\\Validation\\Validators',
|
|
'Respect\\Validation\\Validators',
|
|
],
|
|
])
|
|
);
|
|
v::something(); // Try to load "My\Validation\Validators\Something" if any
|
|
```
|