Skip to content

Commit

Permalink
Improve descriptions and examples
Browse files Browse the repository at this point in the history
  • Loading branch information
jamielsharief committed Jul 27, 2020
1 parent 82a12f0 commit 94d5518
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 7 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## Added

- Added `Validator` object to validate arrays of data
- Added `Validator` object to validate arrays of data (this is being separated from the Model thus allowing it be be completely reusable)
- Added `Validate::notEmpty`

## [1.1.0] - 2020-07-15
Expand Down
41 changes: 38 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
[![build](https://travis-ci.org/originphp/validation.svg?branch=master)](https://travis-ci.org/originphp/validation)
[![coverage](https://coveralls.io/repos/github/originphp/validation/badge.svg?branch=master)](https://coveralls.io/github/originphp/validation?branch=master)

This is the new Validation library.
This provides a `Validation` library and the `Validator` class for setting up and running validation rules on arrays of data.

## Installation

Expand All @@ -14,15 +14,50 @@ To install this package
$ composer require originphp/validation
```

## Usage
## Validation Library

For example

```php
$bool = Validation::ip('192.168.1.25');
```

## Rules
The full list of validation rules are listed below.

## Validator

```php
$validator = new Validator();

$validator->add('name','required')
->add('email',[
'optional', // rule name
'email' => [
'rule' => 'email' // rule name
'message' => 'Invalid email address' // custom message
]
]);
```

You can also use custom validation rules

```php
$validator->add('first_name', 'uniqueRuleName', [
'rule' => [new NameAssert(),'firstName'] // [$object,'method']
]);
```

Or to use a closure

```php
$validator->add('status', 'uniqueRuleName', [
'rule' => function ($value) {
return $value === 'active';
}
]);
```

## Validation Rules

### accepted

Expand Down
24 changes: 21 additions & 3 deletions src/Validator.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,12 @@ class Validator
*
* $validator
* ->add('name','notEmpty')
* ->add('email','email', ['message'=>'Invalid Email address']) //uses the alias for the rule name
* ->add('code','valid', ['rule'=> ['in',[1,2,3]]]) // set the rule
* ->add('email','email', [
* 'message'=>'Invalid Email address'
* ]) // uses the alias for the rule name
* ->add('code','valid', [
* 'rule'=> ['in',[1,2,3]]
* ]) // set the rule
*
* // to create multiple validation rules on the same field (second param is array)
*
Expand All @@ -95,6 +99,20 @@ class Validator
* ]
* ]);
*
* // you can also use objects
*
* $validator->add('status', 'uniqueName', [
* 'rule' => [$this, 'method']
* ]);
*
* // and closures
*
* $validator->add('status', 'uniqueName', [
* 'rule' => function ($value) {
* return $value === 'active';
* }
* ]);
*
* @param string $field name of the field to validate
* @param string|array $name rule name for single rule or an array for multiple rules
* @param array $options options
Expand Down Expand Up @@ -212,7 +230,6 @@ public function validate(array $data, bool $isNewRecord = true): array

foreach ($this->validationRules as $field => $validationRules) {
$present = array_key_exists($field, $data);

$value = $data[$field] ?? null;
$isEmpty = ! Validation::notEmpty($value);

Expand Down Expand Up @@ -284,6 +301,7 @@ protected function isValid($value, array $validationRule): bool
// handle args etc
$rule = $validationRule['rule'];
$args = [$value];

if (is_array($validationRule['rule'])) {
$rule = array_shift($validationRule['rule']);
}
Expand Down

0 comments on commit 94d5518

Please sign in to comment.