Skip to content

Commit

Permalink
Merge pull request #1 from andrewdyer/version-2
Browse files Browse the repository at this point in the history
Version 2
  • Loading branch information
andrewdyer committed Nov 15, 2018
2 parents 9ba5c46 + f9c29bf commit 9501977
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 41 deletions.
56 changes: 39 additions & 17 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,38 +23,60 @@ composer require andrewdyer/slim3-validator

## Usage

Attach a new instance of `Anddye\Validation\Validator` to your applications container so
it can be accessed anywhere you need.

```php
$app = new \Slim\App();
use Slim\App;
use Anddye\Validation\Validator;
use Respect\Validation\Validator as v;

$app = new App();

$container = $app->getContainer();

$container['validator'] = function () {
return new Anddye\Validation\Validator();
$container['validationService'] = function () {
return new Validator();
};

$app->run();
```

You can easily validate your form inputs using the validate() helper. Assign to a
variable the `validate()` method - passing in the $request object as well as an array
where the array key represents the name of the field and the array value represents
the validation rules:

```php
$app->get('/', function (Request $request, Response $response) use ($container) {
$validation = $container['validator']->validate($request, [
$validation = $container['validationService']->validate($request, [
'email' => v::email()->length(1, 254)->notEmpty(),
'forename' => v::alpha()->length(1, 100)->notEmpty()->noWhitespace(),
'password' => v::length(8, 100)->notEmpty(),
'surname' => v::alpha()->length(1, 100)->notEmpty()->noWhitespace(),
'username' => v::alnum()->length(1, 32)->notEmpty()->noWhitespace(),
]);

// ...
if (!$validation->hasPassed()) {
// Validation has not passed
} else {
// Validation has passed
}
});

$app->run();
```

Attach a new instance of `Anddye\Validation\Validator;` to your applications container so
it can be accessed anywhere you need.

```php
$container['validationService'] = function () {
return new \Anddye\Validation\Validator();
};
```

You can easily validate your form inputs using the validate() helper. Assign to a
variable the `validate()` method - passing in the $request object as well as an array
where the array key represents the name of the field and the array value represents
the validation rules.

```php
$validation = $container['validationService']->validate($request, [
'email' => v::email()->length(1, 254)->notEmpty(),
'forename' => v::alpha()->length(1, 100)->notEmpty()->noWhitespace(),
'password' => v::length(8, 100)->notEmpty(),
'surname' => v::alpha()->length(1, 100)->notEmpty()->noWhitespace(),
'username' => v::alnum()->length(1, 32)->notEmpty()->noWhitespace(),
]);
```

Respect\Validation is namespaced, but you can make your life easier by importing a
Expand Down
6 changes: 3 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@
"source": "https://github.com/andrewdyer/slim3-validator.git"
},
"require": {
"php": ">=7.1.14",
"slim/slim": "^3.10",
"respect/validation": "^1.1"
"php": ">=7.0.0",
"slim/slim": "^3.11.0",
"respect/validation": "^1.1.29"
},
"autoload": {
"psr-4": {
Expand Down
40 changes: 19 additions & 21 deletions src/Validation/Validator.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,67 +2,65 @@

namespace Anddye\Validation;

use Psr\Http\Message\ServerRequestInterface as Request;
use Psr\Http\Message\RequestInterface as Request;
use Respect\Validation\Exceptions\NestedValidationException;

/**
* Class Validator.
*
* @author Andrew Dyer <[email protected]>
*
* @category Validation
*
* @see https://github.com/andrewdyer/slim3-validator
*/
class Validator
{
/** @var array */
private $_errors = [];
/**
* @var array
*/
private $errors = [];

/**
* @return array
*/
public function getErrors(): array
{
return $this->_errors;
return $this->errors;
}

/**
* @return bool
*/
public function hasPassed(): bool
{
return empty($this->_errors);
return empty($this->errors);
}

/**
* @param string $key
* @param string $field
* @param array $messages
*
* @return $this
* @return Validator
*/
public function setErrors(string $key, array $messages): self
public function setErrors(string $field, array $messages): self
{
$this->_errors[$key] = $messages;
$this->errors[$field] = $messages;

return $this;
}

/**
* @param string $string
*
* @return string
*/
protected function formatName(string $string): string
{
return str_replace(['-', '_'], ' ', ucfirst(strtolower($string)));
}

/**
* @param Request $request
* @param array $rules
*
* @return $this
* @return Validator
*/
public function validate(Request $request, array $rules): self
{
foreach ($rules as $field => $validator) {
try {
$name = $this->formatName($field);
$name = str_replace(['-', '_'], ' ', ucfirst(strtolower($field)));
$validator->setName($name)->assert($request->getParam($field));
} catch (NestedValidationException $ex) {
$this->setErrors($field, $ex->getMessages());
Expand Down

0 comments on commit 9501977

Please sign in to comment.