respect-validation/docs/custom-validators.md
Alexandre Gomes Gaigalas d9cdc118b2 Introduce REUSE compliance
This commit introduces REUSE compliance by annotating all files
with SPDX information and placing the reused licences in the
LICENSES folder.

We additionally removed the docheader tool which is made obsolete
by this change.

The main LICENSE and copyright text of the project is now not under
my personal name anymore, and it belongs to "The Respect Project
Contributors" instead.

This change restores author names to several files, giving the
appropriate attribution for contributions.
2026-01-21 06:28:11 +00:00

1.4 KiB

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:

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
{
    protected 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.

Factory::setDefaultInstance(
    (new Factory())
        ->withNamespace('My\\Validation\\Validators')
);
v::something(); // Try to load "My\Validation\Validators\Something" if any