mirror of
https://github.com/Respect/Validation.git
synced 2024-06-07 00:02:33 +02:00
66faefd695
After many refactorings, no rules use the previous validation engine. That means we can remove the unused code from the repository and switch from the previous to the new validation engine everywhere. This commit will also soft deprecate the methods "validate()", and "check()" in all the rules and the "assert()" in all rules but the Validator itself. That means using those methods will still be allowed, but static analysis tools might complain. This is a big step toward releasing the next major version, as the code is pretty much the way it should be when I release the next version. There's some documentation to be updated, and I would like to change the behavior of a couple of rules. Signed-off-by: Henrique Moody <henriquemoody@gmail.com>
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 Validatable
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