|
| 1 | +<p align="center"><h1 align="center">:panda_face: PHP VALIDATION TRAIT :panda_face:</h1></p> |
| 2 | +<p align="center"> |
| 3 | + <a href="https://packagist.org/packages/b-tokman/validation" target="_blank"> |
| 4 | + <img src="https://badgen.net/packagist/lang/b-tokman/validation" alt="Programming language"> |
| 5 | + </a> |
| 6 | + <a href="https://scrutinizer-ci.com/g/bTokman/validation/?branch=master" target="_blank"> |
| 7 | + <img src="https://scrutinizer-ci.com/g/bTokman/validation/badges/quality-score.png?b=master" alt="Scrutinizer code quality"> |
| 8 | + </a> |
| 9 | + <a href="https://travis-ci.org/github/bTokman/validation" target="_blank"> |
| 10 | + <img src="https://badgen.net/travis/bTokman/validation" alt="Travis build"> |
| 11 | + </a> |
| 12 | +</p> |
| 13 | + |
| 14 | +This is simple PHP standalone Trait, that can be used in any object. The validation process based on the [Symfony Validation Component](https://symfony.com/doc/current/components/validator.html) |
| 15 | + |
| 16 | +# :rocket: Installation |
| 17 | +This library requeired [PHP](https://www.php.net) version `7.2` or highter. And [composer](https://getcomposer.org/) - package manger for PHP. |
| 18 | + |
| 19 | +```sh |
| 20 | + $ composer require b-tokman/validation |
| 21 | +``` |
| 22 | + |
| 23 | +# :bulb: Usage |
| 24 | + |
| 25 | +After the installation you'll be able to use the `bTokman\validation\ValidationTrait` trait in your app. |
| 26 | + |
| 27 | +**You cannot override a trait's property in the class where the trait is used.** |
| 28 | +However, you can override a trait's property in a class that extends the class where the trait is used. |
| 29 | + |
| 30 | +- Start using trait in your base class |
| 31 | +- Declare validation rules in your extended class. [List of availible Rules](https://symfony.com/doc/current/validation.html#constraints) |
| 32 | +- On a new instance of your class just call method `validate`. |
| 33 | + |
| 34 | +**The validation result is** |
| 35 | +- `array` of errors `[[fieldName] => [errorMessage1, errorMessag2, ...]` . |
| 36 | +- `null` if the validation was passed. |
| 37 | + |
| 38 | + |
| 39 | + |
| 40 | +```php |
| 41 | +class BaseObject |
| 42 | +{ |
| 43 | + use bTokman\validation\ValidationTrait; |
| 44 | +} |
| 45 | + |
| 46 | +...... |
| 47 | + |
| 48 | +class ValidationObject extends BaseObject |
| 49 | +{ |
| 50 | + public $validationRules = [ |
| 51 | + 'password' => [NotBlank::class, [Length::class, ['min' => 8]]], |
| 52 | + ]; |
| 53 | + |
| 54 | + public $password; |
| 55 | +} |
| 56 | + |
| 57 | +...... |
| 58 | + |
| 59 | +$object = new ValidationObject(); |
| 60 | + |
| 61 | +$result = $object->validate(); |
| 62 | + |
| 63 | + |
| 64 | +``` |
| 65 | + |
| 66 | +```php |
| 67 | + |
| 68 | +class ValidationObject |
| 69 | +{ |
| 70 | + use ValidationTrait; |
| 71 | + |
| 72 | + public function __construct() |
| 73 | + { |
| 74 | + $this->validationRules = [ |
| 75 | + 'password' => [NotBlank::class, [Length::class, ['min' => 8]]], |
| 76 | + ]; |
| 77 | + } |
| 78 | + |
| 79 | + public $password; |
| 80 | +} |
| 81 | + |
| 82 | +$object = new ValidationObject(); |
| 83 | + |
| 84 | +$result = $object->validate(); |
| 85 | + |
| 86 | +``` |
0 commit comments