From 941c408e845c69a3b4fdee625c744cb2857ef210 Mon Sep 17 00:00:00 2001 From: "Dyer, Andy" Date: Thu, 15 Nov 2018 13:46:33 +0000 Subject: [PATCH 1/3] Updated Validator.php --- src/Validation/Validator.php | 40 +++++++++++++++++------------------- 1 file changed, 19 insertions(+), 21 deletions(-) diff --git a/src/Validation/Validator.php b/src/Validation/Validator.php index 28248b0..9d7d792 100644 --- a/src/Validation/Validator.php +++ b/src/Validation/Validator.php @@ -2,23 +2,31 @@ 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 + * + * @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; } /** @@ -26,43 +34,33 @@ public function getErrors(): array */ 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()); From 9a4e3c88436b99302312dca998ea1b9893f2d754 Mon Sep 17 00:00:00 2001 From: "Dyer, Andy" Date: Thu, 15 Nov 2018 13:46:46 +0000 Subject: [PATCH 2/3] Updated README.md --- README.md | 56 ++++++++++++++++++++++++++++++++++++++----------------- 1 file changed, 39 insertions(+), 17 deletions(-) diff --git a/README.md b/README.md index 4a487c9..0e4afe7 100644 --- a/README.md +++ b/README.md @@ -23,29 +23,21 @@ 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(), @@ -53,8 +45,38 @@ $app->get('/', function (Request $request, Response $response) use ($container) '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 From f9c29bf9b6049a36b324aaa40f3da9308d2f39ce Mon Sep 17 00:00:00 2001 From: "Dyer, Andy" Date: Thu, 15 Nov 2018 13:47:06 +0000 Subject: [PATCH 3/3] Updated dependencies --- composer.json | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/composer.json b/composer.json index 3ced2da..e7de2ee 100644 --- a/composer.json +++ b/composer.json @@ -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": {