Custom Rules
The library provides flexibility by allowing you to extend its functionality with custom validation rules. Here’s the process for defining your own rules.
Create a Rule Class
- Your rule class must implement the
Rule
interface provided by the library. - Use constructor arguments to allow for customization of your rule’s behavior (e.g., the
transparency
option in theHexColor
example). - Include the
#[\Attribute]
annotation if you want to use your rule as an attribute (optional). - Implement the
validator()
method, returning the fully qualified name of your validator class that you’ll create next.
Example:
Create a Validator Class
- Your validator should be a callable class (often a single-method class, as in the example).
- The validator’s __invoke() method will be called during the validation process. It receives:
$value
: The value being validated.$rule
: An instance of your rule class.
- Inside the
__invoke()
method, perform the necessary validation logic. If the validation fails, throw aValueRuleViolation
exception.
Example:
If your validator requires some dependencies, please refer to the Validator Dependencies documentation section.
Use Your Custom Rule
Instantiate your custom rule object and include it in your validation rules array, just like any built-in rule.
Example:
Use Built-in or Custom Normalizers (Optional)
The library allows you to apply normalizers to your data before validation, providing a way to preprocess and clean up values. To use normalizers with your custom rule:
- Implement the
Normalizable
interface: Add theNormalizable
interface to your rule class. - Implement the
normalizers()
method: This method should return an array of normalizer objects provided by the library or your own custom normalizers.
You can use one of the built-in normalizers (which are simple callables) or create your own.
Binary
: Converts values to booleantrue
orfalse
. Recognizes various representations of “on” (e.g., “On”, “Yes”, “1”) and “off” (e.g., “Off”, “No”, “0”).DateTime
: Converts value to aDateTimeImmutable
instance.Lower
: Converts all characters within a string to lowercase.Upper
: Converts all characters within a string to uppercase.Numeric
: Converts numeric string to a number.Spaceless
: Removes all space characters from a string.Trim
: Removes leading and trailing whitespace from a string.
Normalizers modify the value before it reaches your validator. This can be useful for tasks like trimming whitespace, converting to lowercase, or other transformations.
Normalizers do not change the original data; they provide a normalized copy for the validation process.
Validator Dependencies
When creating custom validators that have external dependencies (like database connections or other services), you’ll need a way to provide these dependencies to your validator instances. The library offers flexibility in how you approach this.
Let’s assume you have a custom rule UniqueConstraint
and its corresponding validator UniqueConstraintValidation
,
which requires a PDO
instance.
Your rule:
Your validator:
Pass In a Map (Simplest)
For straightforward use cases, create a map of validator class names to their instances and pass this directly to the Validator constructor.
Implement Your Registry
Create a custom Registry
implementation to manage the creation of validator instances and their dependencies.
Your registry:
Use:
Implement a Container Adapter
If you’re using a PSR-11 compatible dependency injection container (or any other DI container), create an adapter to leverage it.
Your adapter:
Use: