VOOZH about

URL: https://deepwiki.com/hypervel/validation/5.2-extensions-and-customization

⇱ Extensions and Customization | hypervel/validation | DeepWiki


Loading...
Menu

Extensions and Customization

Purpose and Scope

This page documents the extension system in hypervel/validation, which allows developers to register custom validation rules and message replacers beyond the 50+ built-in rules. The extension system supports three types of custom rules (regular, implicit, and dependent), custom message replacers, and fallback messages.

For information about using extensions within validators, see Creating Validators. For documentation on custom rule objects and the Rule interface, see Custom Validation Rules.


Extension System Architecture

The extension system operates through a two-tier architecture where the Factory serves as the central registry for extensions, which are then applied to individual Validator instances upon creation.

Extension Flow


Sources: src/Factory.php80-108 src/Factory.php134-149 src/Validator.php1139-1222


Extension Types

The validation system supports three distinct types of custom extensions, each with different validation behavior.

Extension Type Comparison

Extension TypeRuns When Field MissingRuns When Field NullUse Case
RegularNoNo (unless not nullable)Standard validation rules
ImplicitYesYesRules that require field presence (like required)
DependentDependsDependsRules that reference other fields in parameters

Extension Type Details


Regular Extensions are standard validation rules that only execute when the field is present and not null (unless marked as nullable). They are registered via Factory::extend() src/Factory.php154-161

Implicit Extensions always execute regardless of whether the field exists or is null. They are used for rules that determine field presence requirements, similar to the built-in required rule. Registered via Factory::extendImplicit() src/Factory.php166-173

Dependent Extensions are rules that reference other field names in their parameters (like same:other_field). The validator automatically expands wildcard parameters for these rules. Registered via Factory::extendDependent() src/Factory.php178-185

Sources: src/Factory.php154-185 src/Validator.php1139-1172 src/Validator.php163-188 src/Validator.php195-236


Registering Extensions via Factory

Extensions are registered on the Factory instance, which then applies them to all validators it creates.

Registration Methods


Extension Signature Options

Extensions can be defined as either closures or class-based strings:

Closure-based Extension:


Class-based Extension:


The extension receives four parameters:

  • $attribute - The attribute name being validated
  • $value - The value being validated
  • $parameters - Array of rule parameters
  • $validator - The Validator instance

Fallback Messages

When registering an extension, an optional fallback message can be provided as the third parameter. This message is used when no custom message is defined:


The fallback message is stored in snake_case format in the fallbackMessages array src/Factory.php159

Sources: src/Factory.php152-193 src/Factory.php22-53


Extension Application to Validators

When the Factory creates a validator via make(), it applies all registered extensions through the addExtensions() method.

Extension Application Process


Key Extension Application Methods

The Validator class provides both singular and plural methods for adding extensions at the instance level:

MethodPurposeKey Behavior
addExtensions(array)Add multiple regular extensionsConverts keys to snake_case src/Validator.php1139-1148
addExtension(string, $ext)Add single regular extensionConverts rule name to snake_case src/Validator.php1177-1180
addImplicitExtensions(array)Add multiple implicit extensionsCalls addExtensions() then adds to implicitRules src/Validator.php1152-1160
addImplicitExtension(string, $ext)Add single implicit extensionConverts to snake_case, adds to implicitRules src/Validator.php1185-1190
addDependentExtensions(array)Add multiple dependent extensionsCalls addExtensions() then adds to dependentRules src/Validator.php1165-1172
addDependentExtension(string, $ext)Add single dependent extensionConverts to snake_case, adds to dependentRules src/Validator.php1195-1200
addReplacers(array)Add multiple message replacersConverts keys to snake_case src/Validator.php1205-1214
addReplacer(string, $rep)Add single message replacerConverts rule name to snake_case src/Validator.php1219-1222

All extension keys are normalized to snake_case during registration to ensure consistent lookup.

Sources: src/Validator.php1139-1222 src/Factory.php134-149


Extension Invocation During Validation

When a validator encounters a custom rule during validation, it invokes the extension through a dynamic method call mechanism.

Extension Invocation Flow


Extension Lookup and Execution

The Validator::__call() magic method handles dynamic method calls for custom extensions src/Validator.php1416-1429:


The callExtension() method determines whether the extension is a closure or class-based and invokes it accordingly src/Validator.php1385-1397:

  • Closure extensions: Called directly with array_values($parameters)
  • Class-based extensions: Resolved via callClassBasedExtension() which uses the DI container to instantiate the class src/Validator.php1402-1409

Extension Parameter Structure

Extensions receive parameters in a specific structure:


  • $attribute - String, the field name being validated
  • $value - Mixed, the field value
  • $parameters - Array, parsed rule parameters (e.g., ['5'] for divisible_by:5)
  • $validator - Validator instance, provides access to all validation data and methods

Sources: src/Validator.php1416-1429 src/Validator.php1385-1409 src/Validator.php554-606


Message Replacers

Message replacers customize how error message placeholders are replaced for specific rules. They supplement the standard placeholder replacement system.

Replacer Registration and Invocation


Replacer Invocation

Custom replacers are invoked during the message replacement phase in the FormatsMessages trait. The trait checks for a custom replacer method in this order:

  1. Check Validator::$replacers array for custom replacer (closure or class-based)
  2. Check for replace{Rule} method in FormatsMessages trait
  3. Use standard :attribute, :value replacements if no custom replacer exists

Custom replacers receive the same four parameters as extensions and return a modified message string.

Replacer Example


This replacer would replace :divisor in the message "The :attribute must be divisible by :divisor." with the actual parameter value.

Sources: src/Factory.php190-193 src/Validator.php1205-1222


Instance-Level Customization

While the Factory manages extensions at the application level, individual Validator instances can also receive custom extensions directly.

Direct Validator Customization Methods

The Validator class exposes public methods that allow runtime customization:

MethodPurposeTypical Use Case
setCustomMessages(array)Override error messages for this validatorOne-off message customization src/Validator.php1227-1232
setAttributeNames(array)Set custom attribute display namesUser-friendly field names src/Validator.php1237-1242
addCustomAttributes(array)Add to existing custom attributesIncremental attribute naming src/Validator.php1247-1252
setValueNames(array)Set custom value display namesEnum/status display src/Validator.php1267-1272
addCustomValues(array)Add to existing custom valuesIncremental value naming src/Validator.php1277-1282
setFallbackMessages(array)Set fallback messages for custom rulesApplied by Factory src/Validator.php1287-1290

Customization Flow


Instance-level customizations are particularly useful for:

  • Per-request message overrides without modifying application-wide settings
  • Dynamic attribute naming based on runtime context
  • Specialized validation in specific controllers or use cases

Sources: src/Validator.php1227-1290 src/Factory.php80-108


Complete Extension Example

The following example demonstrates all extension types working together:

Extension Registration


Extension Usage


Extension Processing Flow


Sources: src/Factory.php152-193 src/Validator.php1139-1222 src/Validator.php554-606


Class-Based Extensions

For complex validation logic, extensions can be implemented as class methods and registered by class string reference.

Class-Based Extension Structure


Class-Based Extension Implementation


The validator uses Str::parseCallback() to extract the class name and method src/Validator.php1404 then resolves the class via the PSR-11 container src/Validator.php1407-1408 This allows class-based extensions to leverage dependency injection for accessing services like database connections or external APIs.

Sources: src/Validator.php1402-1409 src/Factory.php152-161


Summary

The extension system in hypervel/validation provides a flexible mechanism for adding custom validation logic:

  • Three Extension Types: Regular, implicit, and dependent extensions support different validation scenarios
  • Two Registration Levels: Factory for application-wide extensions, Validator for instance-specific customization
  • Two Implementation Styles: Closure-based for simple logic, class-based for complex validation with dependency injection
  • Message Customization: Custom replacers and fallback messages integrate with the hierarchical message system
  • Consistent API: Snake_case normalization and standardized parameter structure across all extension types

The system maintains separation between extension registration (Factory) and extension execution (Validator), enabling both global configuration and per-validator customization while keeping the core validation engine clean and extensible.

Sources: src/Factory.php1-260 src/Validator.php1-1430

Refresh this wiki

On this page