11# Using Yet Another PHP Validator
22
3- - Usage
4- - Fluent
5- - Dependency Injection
6- - Methods
7- - assert
8- - validate
9- - getRules
10- - addRule
11- - Exception Handling
12- - Custom Exception Messages
3+ - [ Usage] ( #usage )
4+ - [ Fluent] ( #fluent )
5+ - [ Dependency Injection] ( #dependency-injection )
6+ - [ Methods] ( #methods )
7+ - [ assert] ( #assert )
8+ - [ validate] ( #validate )
9+ - [ getRules] ( #getrules )
10+ - [ addRule] ( #addrule )
11+ - [ Error Handling] ( #error-handling )
12+ - [ Custom Error Messages] ( #custom-error-messages )
1313
1414## Usage
1515
16- This library allows you to use validate data in two different ways:
16+ This library allows you to validate data in two different ways:
1717- In a fluent way, making use of magic methods. The goal is to be able to create a set of rules with minimum setup;
1818- In a traditional way, making use of dependency injection. You may not like the fluent approach, and prefer to work this way.
1919
@@ -71,7 +71,7 @@ This method throws a `ValidationException` when a rule fails, otherwise nothing
7171assert(mixed $value, string $name): void;
7272```
7373
74- An example on how to handle an exception :
74+ An example on how to handle an error :
7575
7676``` php
7777use ProgrammatorDev\YetAnotherPhpValidator\Exception\ValidationException;
@@ -93,9 +93,11 @@ catch (ValidationException $exception) {
9393 echo $exception->getMessage(); // The "Latitude" value should be between "-90" and "90", "100" given.
9494}
9595```
96+ > ** Note**
97+ > Check the [ Error Handling] ( #error-handling ) section for more information.
9698
9799> ** Note**
98- > The example only shows one usage approach, but both Fluent and Dependency Injection usage approaches should work the same.
100+ > The example only shows one usage approach, but both Fluent and Dependency Injection should work the same.
99101> Check the [ Usage] ( #usage ) section for more information.
100102
101103### ` validate `
@@ -117,7 +119,7 @@ if (!Validator::range(-90, 90)->validate($latitude)) {
117119```
118120
119121> ** Note**
120- > The example only shows one usage approach, but both Fluent and Dependency Injection usage approaches should work the same.
122+ > The example only shows one usage approach, but both Fluent and Dependency Injection should work the same.
121123> Check the [ Usage] ( #usage ) section for more information.
122124
123125### ` getRules `
@@ -148,7 +150,7 @@ print_r($validator->getRules());
148150```
149151
150152> ** Note**
151- > The example only shows one usage approach, but both Fluent and Dependency Injection usage approaches should work the same.
153+ > The example only shows one usage approach, but both Fluent and Dependency Injection should work the same.
152154> Check the [ Usage] ( #usage ) section for more information.
153155
154156### ` addRule `
@@ -180,16 +182,75 @@ function calculateDiscount(float $price, float $discount, string $type): float
180182```
181183
182184> ** Note**
183- > The example only shows one usage approach, but both Fluent and Dependency Injection usage approaches should work the same.
185+ > The example only shows one usage approach, but both Fluent and Dependency Injection should work the same.
184186> Check the [ Usage] ( #usage ) section for more information.
185187
186- ## Exception Handling
188+ ## Error Handling
189+
190+ When using the [ ` assert ` ] ( #assert ) method, an exception is thrown when a rule fails.
191+
192+ Each rule has a unique exception, formed by the name of the rule followed by the word Exception, like ` RuleNameException ` .
193+ The following shows an example:
194+
195+ ``` php
196+ use ProgrammatorDev\YetAnotherPhpValidator\Exception;
197+ use ProgrammatorDev\YetAnotherPhpValidator\Validator;
198+
199+ try {
200+ Validator::range(-90, 90)->assert($latitude, 'Latitude');
201+ Validator::range(-180, 180)->assert($longitude, 'Longitude');
202+ Validator::notBlank()->choice(['METRIC', 'IMPERIAL'])->assert($unitSystem, 'Unit System');
203+ }
204+ catch (Exception\RangeException $exception) {
205+ // Do something when Range fails
206+ }
207+ catch (Exception\NotBlankException $exception) {
208+ // Do something when NotBlank fails
209+ }
210+ catch (Exception\ChoiceException $exception) {
211+ // Do something when Choice fails
212+ }
213+ ```
214+
215+ To catch all errors with a single exception, you can use the ` ValidationException ` :
216+
217+ ``` php
218+ use ProgrammatorDev\YetAnotherPhpValidator\Exception\ValidationException;
219+ use ProgrammatorDev\YetAnotherPhpValidator\Validator;
220+
221+ try {
222+ Validator::range(-90, 90)->assert($latitude, 'Latitude');
223+ Validator::range(-180, 180)->assert($longitude, 'Longitude');
224+ Validator::notBlank()->choice(['METRIC', 'IMPERIAL'])->assert($unitSystem, 'Unit System');
225+ }
226+ catch (ValidationException $exception) {
227+ // Do something when a rule fails
228+ echo $exception->getMessage();
229+ }
230+ ```
231+
232+ When using both the [ ` assert ` ] ( #assert ) or [ ` validate ` ] ( #validate ) methods,
233+ an ` UnexpectedValueException ` is thrown when the provided input data is not valid to perform the validation.
234+
235+ For example, when trying to compare a date with a string:
236+
237+ ``` php
238+ use ProgrammatorDev\YetAnotherPhpValidator\Exception\UnexpectedValueException;
239+ use ProgrammatorDev\YetAnotherPhpValidator\Validator;
240+
241+ try {
242+ Validator::greaterThanOrEqual(new DateTime('today'))->validate('alpha');
243+ }
244+ catch (UnexpectedValueException $exception) {
245+ echo $exception->getMessage(); // Cannot compare a type "string" with a type "DateTime".
246+ }
247+ ```
187248
188- ## Custom Exception Messages
249+ ## Custom Error Messages
189250
190251All rules have at least one error message that can be customized (some rules have more than one error message for different case scenarios).
191252
192- Every message has a list of dynamic parameters to help create an intuitive error text (like the invalid value, constraints, names, and others).
253+ Every message has a list of dynamic parameters to help create an intuitive error (like the invalid value, constraints, names, and others).
193254To check what parameters and messages are available, look into the Options section in the page of a rule.
194255Go to [ Rules] ( 03-rules.md ) to see all available rules.
195256
@@ -203,5 +264,5 @@ Validator::choice(
203264 message: '"{{ value }}" is not a valid {{ name }}! You must select one of {{ constraints }}.'
204265)->assert('yellow', 'color');
205266
206- // "yellow" is not a valid color! You must select one of [red, green, blue].
267+ // Throws: "yellow" is not a valid color! You must select one of [red, green, blue].
207268```
0 commit comments