Download the PHP package programmatordev/fluent-validator without Composer
On this page you can find all versions of the php package programmatordev/fluent-validator. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download programmatordev/fluent-validator
More information about programmatordev/fluent-validator
Files in programmatordev/fluent-validator
Package fluent-validator
Short Description A Symfony Validator wrapper that enables fluent-style validation for raw values, offering an easy-to-use and intuitive API to validate user input or other data in a concise and readable manner.
License MIT
Informations about the package fluent-validator
Fluent Validator
A Symfony Validator wrapper that enables fluent-style validation for raw values, offering an easy-to-use and intuitive API to validate user input or other data in a concise and readable manner.
Features
- 🌊 Fluent-style validation: Chain validation methods for better readability and flow.
- 🤘 Constraints autocompletion: Enables IDE autocompletion for available constraints.
- 🔥 Three validation methods: Use
validate
,assert
, orisValid
based on the context (i.e., collect errors or throw exceptions). - ⚙️ Custom constraints: Easily integrate custom validation logic with Symfony's Validator system.
- 💬 Translations support: Translate validation error messages into multiple languages.
Table of Contents
- Installation
- Usage
- Constraints
- Methods
- validate
- assert
- isValid
- getConstraints
- addNamespace
- setTranslator
- Custom Constraints
- Translations
Requirements
- PHP 8.2 or higher.
Installation
Install via Composer:
Usage
Simple usage example:
Constraints autocompletion is available in IDEs like PhpStorm. The method names match Symfony constraints but with a lowercase first letter:
NotBlank
=>notBlank
All
=>all
PasswordStrength
=>passwordStrength
- ...and so on.
For all available constraints, check the Constraints section.
For all available methods, check the Methods section.
There is also a section for Translations.
Constraints
All available constraints can be found on the Symfony Validator documentation.
For custom constraints, check the Custom Constraints section.
Methods
validate
Returns a ConstraintViolationList
object, acting as an array of errors.
assert
Throws a ValidationFailedException
when validation fails.
isValid
Returns a bool
indicating if the value is valid.
getConstraints
Returns an array with all added constraints.
It is useful for Composite
constraints (i.e., a constraint that is composed of other constraints)
and keeps the fluent-style validation:
addNamespace
Used to add namespaces for custom constraints.
Check the Custom Constraints section.
setTranslator
Used to add a translator for validation error message translations.
Check the Translations section.
Custom Constraints
If you need a custom constraint, follow the Symfony Validator documentation: Creating Custom Constraints.
Example: Creating a ContainsAlphanumeric
Constraint
1. Create a Constraint Class
This class defines the error message and configurable options.
2. Create the Validator Class
The validator checks if the value complies with the constraint rules.
3. Register the Constraint Namespace
Register the namespace where the custom constraints will be located in your project.
You can have multiple constraints in the same namespace or have multiple namespaces.
[!NOTE] Custom constraints will not be suggested in IDE autocompletion.
Translations
Set a global translator to handle error message translations.
To add your own translations, you can integrate a custom translator.
Contributing
Any form of contribution to improve this library (including requests) will be welcome and appreciated. Make sure to open a pull request or issue.
License
This project is licensed under the MIT license. Please see the LICENSE file distributed with this source code for further information regarding copyright and licensing.
All versions of fluent-validator with dependencies
symfony/config Version ^7.2
symfony/translation Version ^7.2
symfony/validator Version ^7.2