Skip to content

Commit c8b47c1

Browse files
committed
docs: clarify validator usage
1 parent 7a20bf8 commit c8b47c1

1 file changed

Lines changed: 49 additions & 0 deletions

File tree

README.md

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ offering an easy-to-use and intuitive API to validate user input or other data i
2121
## Table of Contents
2222

2323
- [Installation](#installation)
24+
- [When to use it](#when-to-use-it)
2425
- [Usage](#usage)
2526
- [Constraints](#constraints)
2627
- [Methods](#methods)
@@ -46,6 +47,13 @@ Install via [Composer](https://getcomposer.org/):
4647
composer require programmatordev/fluent-validator
4748
```
4849

50+
## When to use it
51+
52+
Use Fluent Validator when you want Symfony Validator constraints for raw values without setting up object metadata, attributes, forms, or a larger validation layer.
53+
It is useful for small input checks, command arguments, request fragments, webhook payload values, configuration values, and library code.
54+
55+
This package does not replace Symfony Validator. It wraps Symfony Validator and keeps its constraints, violation objects, groups, translations, and custom constraint model.
56+
4957
## Usage
5058

5159
Simple usage example:
@@ -64,7 +72,33 @@ if ($errors->count() > 0) {
6472
}
6573
```
6674

75+
Use `assert` when invalid values should stop the current flow:
76+
77+
```php
78+
use ProgrammatorDev\FluentValidator\Exception\ValidationFailedException;
79+
use ProgrammatorDev\FluentValidator\Validator;
80+
81+
try {
82+
Validator::notBlank()->email()->assert($email, 'email');
83+
}
84+
catch (ValidationFailedException $exception) {
85+
$message = $exception->getMessage();
86+
// "email: This value is not a valid email address."
87+
}
88+
```
89+
90+
Use `isValid` when you only need a boolean:
91+
92+
```php
93+
use ProgrammatorDev\FluentValidator\Validator;
94+
95+
if (!Validator::url()->isValid($website)) {
96+
// handle invalid URL
97+
}
98+
```
99+
67100
Constraint autocompletion is available in IDEs like PhpStorm.
101+
The suggested methods are generated from the installed Symfony Validator constraints.
68102
The method names match Symfony constraints but with a lowercase first letter:
69103

70104
- `NotBlank` => `notBlank`
@@ -78,6 +112,20 @@ For all available methods, check the [Methods](#methods) section.
78112

79113
There is also a section for [Custom Constraints](#custom-constraints) and [Translations](#translations).
80114

115+
### Groups
116+
117+
Validation groups work the same way as in Symfony Validator:
118+
119+
```php
120+
use ProgrammatorDev\FluentValidator\Validator;
121+
122+
$validator = Validator::notBlank(groups: ['Default'])
123+
->email(groups: ['registration']);
124+
125+
$validator->isValid('invalid-email', groups: ['Default']); // true
126+
$validator->isValid('invalid-email', groups: ['registration']); // false
127+
```
128+
81129
## Constraints
82130

83131
All available constraints can be found on the [Symfony Validator documentation](https://symfony.com/doc/current/validation.html#constraints).
@@ -217,6 +265,7 @@ reset(): void
217265
```
218266

219267
Clears globally registered custom constraint namespaces and translator configuration.
268+
Useful when changing global validator configuration in tests, workers, or other long-running PHP processes.
220269

221270
## Custom Constraints
222271

0 commit comments

Comments
 (0)