mirror of
https://github.com/Respect/Validation.git
synced 2026-03-17 15:50:03 +01:00
Besides the interface's name, everything already calls this type "Rule", not "Validatable." This commit puts a stone on it and renames the interface for better naming.
1.3 KiB
1.3 KiB
Custom rules
You can also create and use your own rules. To do this, you will need to create a rule and an exception to go with the rule.
To create a rule, you need to create a class that implements the Rule interface
and is within the Rules namespace. It is convenient to just extend the Simple or
Standard class. When the rule is called the logic inside the validate method will be
executed. Here's how the class should look:
namespace My\Validation\Rules;
use Respect\Validation\Message\Template;
use Respect\Validation\Rules\Core\Simple;
#[Template(
'{{name}} is something',
'{{name}} 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 '{{name}} is not something message would be used then you call the rule
with the not().
All classes in Validation are created by the Factory class. If you want
Validation to execute your rule (or rules) in the chain, you must overwrite the
default Factory.
Factory::setDefaultInstance(
(new Factory())
->withRuleNamespace('My\\Validation\\Rules')
);
v::something(); // Try to load "My\Validation\Rules\Something" if any