Download the PHP package cryonighter/validation-override-bundle without Composer
On this page you can find all versions of the php package cryonighter/validation-override-bundle. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download cryonighter/validation-override-bundle
More information about cryonighter/validation-override-bundle
Files in cryonighter/validation-override-bundle
Package validation-override-bundle
Short Description Symfony bundle for dynamic validation constraint modification via provider pattern
License MIT
Homepage https://github.com/cryonighter/validation-override-bundle
Informations about the package validation-override-bundle
Validation Override Bundle
A Symfony bundle that allows dynamic modification of validation constraints at runtime via a provider pattern, without touching the validated class itself.
Description
Symfony's validator loads constraints from PHP attributes, YAML, or XML and caches them. There is no standard extension point to modify these constraints dynamically — for example, based on runtime conditions, environment flags, or external configuration.
This bundle solves this by intercepting the validator's metadata factory and passing
the loaded ClassMetadata through a chain of DynamicRulesProviderInterface providers
before validation occurs. Each provider can add, remove, or replace constraints for a specific class.
When this bundle is appropriate:
- The validated class belongs to a vendor package and cannot be modified
- Validation rules depend on runtime context (database config, tenant settings, environment flags)
- You want to keep the class clean for the common case and override rules only in specific scenarios
When to consider alternatives:
- The class is yours — use validation groups or a second DTO
- The condition is static — use
#[Assert\When]or#[Assert\Callback] - The team is large — the implicit nature of providers increases cognitive overhead
Requirements
- PHP >= 8.2.0 but the latest stable version of PHP is recommended
- Symfony 8.0 or higher (
symfony/framework-bundle,symfony/validator)
Installation
1. Install via Composer
2. Register the bundle
If Symfony Flex did not register the bundle automatically, add it manually:
No additional configuration is required. The bundle registers the validator decorator automatically.
Usage
Step 1 — Create a provider
Implement DynamicRulesProviderInterface. The bundle will auto-tag all implementations
via _instanceof — no services.yaml entry is needed unless you require a specific priority.
Step 2 — Use the validator as usual
No changes to your validation call are required:
The bundle transparently intercepts the validation pipeline and applies your providers.
Controlling provider order
If multiple providers modify the same class, use priority to define the order.
Higher priority runs first:
How It Works
Symfony builds the validator via ValidatorBuilder::getValidator() internally.
The resulting LazyLoadingMetadataFactory is not exposed as a standalone service
in the DI container and cannot be decorated via standard Symfony means.
This bundle works around this limitation with the following approach:
DynamicValidatorDecoratordecorates thevalidatorservice- On the first validation call, it uses
SymfonyInternalsAccessorto locate and replace theMetadataFactoryInterfaceproperty insideRecursiveValidatorvia reflection DynamicMetadataFactorywraps the original factory — on eachgetMetadataFor()call it:- Asks the original factory for
ClassMetadata(cached as usual by Symfony) - Deep-clones the metadata so providers never mutate the cached original
- Passes the clone through all matching providers
- Returns the modified clone to the validator
- Asks the original factory for
Limitations and Caveats
Implicit behavior
Providers are invisible to standard Symfony tooling. php bin/console debug:validator
will show the original constraints defined on the class, not the modified ones.
This is the main trade-off of this approach — document your providers thoroughly.
Reflection over Symfony internals
SymfonyInternalsAccessor uses reflection on private properties of TraceableValidator
and RecursiveValidator. These are internal implementation details of Symfony and
may change in any release. See UPGRADE.md for the compatibility checklist.
Provider isolation
Each provider receives a fresh deep clone of the original ClassMetadata.
However, if two providers modify the same property of the same class,
the second provider's changes will overwrite the first's — use priority to make the order explicit.
Providers that operate on different properties do not interfere with each other.
Compatibility
| Bundle version | PHP | Symfony |
|---|---|---|
| 0.x | ^8.2 | ^8.0 |
Testing
Contributing
Please see CODE_OF_CONDUCT for details.
Security
If you discover any security related issues, please email [email protected] instead of using the issue tracker.
Credits
License
The MIT License (MIT). Please see License File for more information.