|
| 1 | +--- |
| 2 | +weight: -100 |
| 3 | +--- |
| 4 | + |
| 5 | +# Get started |
| 6 | + |
| 7 | +Welcome to Respect\Validation! |
| 8 | + |
| 9 | +This guide will help you get up and running with the library quickly. |
| 10 | + |
| 11 | +## Installation |
| 12 | + |
| 13 | +To install Respect\Validation, use [Composer](http://getcomposer.org): |
| 14 | + |
| 15 | +```shell |
| 16 | +composer require respect/validation:^3.0 |
| 17 | +``` |
| 18 | + |
| 19 | +Ensure you have PHP 8.5 or above installed. |
| 20 | + |
| 21 | +## Basic usage |
| 22 | + |
| 23 | +The `ValidatorBuilder` (aliased as `v` for convenience) provides a fluent interface for building validators and running them. |
| 24 | + |
| 25 | +### Validating using exceptions |
| 26 | + |
| 27 | +The `assert()` method throws an exception when validation fails. Handle these exceptions with `try/catch` for robust error handling: |
| 28 | + |
| 29 | +```php |
| 30 | +try { |
| 31 | + v::intType()->assert($input); |
| 32 | +} catch (Throwable $exception) { |
| 33 | + echo 'Validation failed: ' . $exception->getMessage(); |
| 34 | +} |
| 35 | +``` |
| 36 | + |
| 37 | +### Validating without exceptions |
| 38 | + |
| 39 | +The `validate()` method returns a `ResultQuery` object that allows you to inspect and display validation results: |
| 40 | + |
| 41 | +```php |
| 42 | +$result = v::intType()->validate($input); |
| 43 | +if (!$result->isValid()) { |
| 44 | + echo 'Validation failed: ' . $result->getMessage(); |
| 45 | +} |
| 46 | +``` |
| 47 | + |
| 48 | +### Validating using booleans |
| 49 | + |
| 50 | +Use the `isValid()` method to check if your input meets specific validation criteria: |
| 51 | + |
| 52 | +```php |
| 53 | +if (!v::intType()->isValid($input)) { |
| 54 | + echo 'The input you gave me is not an integer'; |
| 55 | +} |
| 56 | +``` |
| 57 | + |
| 58 | +## Key Features |
| 59 | + |
| 60 | +### Complex validation |
| 61 | + |
| 62 | +Combine multiple validators for complex validation rules: |
| 63 | + |
| 64 | +```php |
| 65 | +v::numericVal()->positive()->between(1, 255)->assert($input); |
| 66 | +``` |
| 67 | + |
| 68 | +### Custom error messages |
| 69 | + |
| 70 | +Define your own error messages when validation fails: |
| 71 | + |
| 72 | +```php |
| 73 | +v::between(1, 256)->assert($input, '{{subject}} is not what I was expecting'); |
| 74 | +``` |
| 75 | + |
| 76 | +### Custom exceptions |
| 77 | + |
| 78 | +Throw your own exceptions when the validation fails: |
| 79 | + |
| 80 | +```php |
| 81 | +v::between(1, 256)->assert($input, new DomainException('Not within the expected range')); |
| 82 | +``` |
| 83 | + |
| 84 | +### Reusing validators |
| 85 | + |
| 86 | +Create validators once and reuse them across multiple inputs: |
| 87 | + |
| 88 | +```php |
| 89 | +$validator = v::alnum()->lowercase(); |
| 90 | + |
| 91 | +$validator->assert('respect'); |
| 92 | +$validator->assert('validation'); |
| 93 | +``` |
| 94 | + |
| 95 | +## Next steps |
| 96 | + |
| 97 | +- Explore the [Feature Guide](feature-guide.md) for more advanced usage. |
| 98 | +- Check out the [List of Validators by Category](list-of-validators-by-category.md) for a comprehensive list of available validators. |
0 commit comments