From ee11bc950107945d34182a359d6a29a0dec07375 Mon Sep 17 00:00:00 2001 From: Mitchell Klijs Date: Wed, 26 Oct 2016 18:20:24 +0200 Subject: [PATCH 1/5] Laravel Validation Errors Added ability to automatically add all the Laraval Validation errors to a notification message bag or via the facade. --- src/Krucas/Notification/Notification.php | 52 ++++++++++++++++++- .../NotificationServiceProvider.php | 2 + 2 files changed, 52 insertions(+), 2 deletions(-) diff --git a/src/Krucas/Notification/Notification.php b/src/Krucas/Notification/Notification.php index 8e1e4ab..452d484 100644 --- a/src/Krucas/Notification/Notification.php +++ b/src/Krucas/Notification/Notification.php @@ -33,6 +33,20 @@ class Notification */ protected $types = []; + /** + * Default Laravel Validation error type available for new containers. + * + * @var string + */ + protected $defaultLaravelValidationErrorType; + + /** + * Laravel Validation error types for defined containers. + * + * @var array + */ + protected $laravelValidationErrorType = []; + /** * Default format for each container type. * @@ -83,6 +97,8 @@ public function __construct( $defaultContainer, $defaultTypes, $types, + $defaultLaravelValidationErrorType, + $laravelValidationErrorType, $defaultFormat, $format, $defaultFormats, @@ -91,6 +107,8 @@ public function __construct( $this->defaultContainer = $defaultContainer; $this->defaultTypes = $defaultTypes; $this->types = $types; + $this->defaultLaravelValidationErrorType = $defaultLaravelValidationErrorType; + $this->laravelValidationErrorType = $laravelValidationErrorType; $this->defaultFormat = $defaultFormat; $this->format = $format; $this->defaultFormats = $defaultFormats; @@ -136,6 +154,35 @@ public function getContainerTypes($container) return $this->defaultTypes; } + /** + * Set Laravel Validation error type for a container. + * + * @param string $container + * @param array $types + * @return \Krucas\Notification\Notification + */ + public function setContainerLaravelValidationErrorType($container, $type) + { + $this->laravelValidationErrorType[$container] = $type; + + return $this; + } + + /** + * Return Laravel Validation error type for a container. + * + * @param $container + * @return array + */ + public function getContainerLaravelValidationErrorType($container) + { + if (isset($this->laravelValidationErrorType[$container])) { + return $this->laravelValidationErrorType[$container]; + } + + return $this->defaultLaravelValidationErrorType; + } + /** * Set format for a container. * @@ -203,13 +250,13 @@ public function getContainerFormats($container) * @param array $formats * @return \Krucas\Notification\Notification */ - public function addContainer($container, $types = [], $defaultFormat = null, $formats = []) + public function addContainer($container, $types = [], $laravelValidationErrorType = null, $defaultFormat = null, $formats = []) { if (isset($this->containers[$container])) { return $this; } - $this->containers[$container] = new NotificationsBag($container, $types, $defaultFormat, $formats); + $this->containers[$container] = new NotificationsBag($container, $types, $laravelValidationErrorType, $defaultFormat, $formats); $this->containers[$container]->setNotification($this); return $this; @@ -240,6 +287,7 @@ public function container($container = null, Closure $callback = null) $this->addContainer( $container, $this->getContainerTypes($container), + $this->getContainerLaravelValidationErrorType($container), $this->getContainerFormat($container), $this->getContainerFormats($container) ); diff --git a/src/Krucas/Notification/NotificationServiceProvider.php b/src/Krucas/Notification/NotificationServiceProvider.php index a35fbcc..b3bd5a3 100644 --- a/src/Krucas/Notification/NotificationServiceProvider.php +++ b/src/Krucas/Notification/NotificationServiceProvider.php @@ -53,6 +53,8 @@ public function register() $config->get('notification.default_container'), $config->get('notification.default_types'), $config->get('notification.types'), + $config->get('notification.default_laravel_validation_error_type'), + $config->get('notification.laravel_validation_error_type'), $config->get('notification.default_format'), $config->get('notification.format'), $config->get('notification.default_formats'), From 0d2d4b3c4df94a1dac20ff8efb743b3220406c01 Mon Sep 17 00:00:00 2001 From: Mitchell Klijs Date: Tue, 3 Jan 2017 11:12:24 +0100 Subject: [PATCH 2/5] Laravel Validation Errors --- src/Krucas/Notification/NotificationsBag.php | 40 +++++++++++++++++++- src/config/notification.php | 24 ++++++++++++ 2 files changed, 63 insertions(+), 1 deletion(-) diff --git a/src/Krucas/Notification/NotificationsBag.php b/src/Krucas/Notification/NotificationsBag.php index d3b51fc..a492675 100644 --- a/src/Krucas/Notification/NotificationsBag.php +++ b/src/Krucas/Notification/NotificationsBag.php @@ -3,6 +3,7 @@ use Countable; use Illuminate\Contracts\Support\Arrayable; use Illuminate\Contracts\Support\Jsonable; +use Illuminate\Contracts\Validation\Validator as ValidatorContract; use Closure; class NotificationsBag implements Arrayable, Jsonable, Countable @@ -21,6 +22,13 @@ class NotificationsBag implements Arrayable, Jsonable, Countable */ protected $types = array(); + /** + * The Laravel Validation error type + * + * @var string + */ + protected $laravelValidationErrorType; + /** * Array of matcher for extracting types. * @@ -76,10 +84,11 @@ class NotificationsBag implements Arrayable, Jsonable, Countable * @param null $defaultFormat * @param array $formats */ - public function __construct($container, $types = array(), $defaultFormat = null, $formats = array()) + public function __construct($container, $types = array(), $laravelValidationErrorType = null, $defaultFormat = null, $formats = array()) { $this->container = $container; $this->addType($types); + $this->setLaravelValidationErrorType($laravelValidationErrorType); $this->setDefaultFormat($defaultFormat); $this->setFormats($formats); $this->notifications = new Collection(); @@ -122,6 +131,18 @@ public function addType($type) return $this; } + /** + * Set the Laravel Validation error type + * + * @param string $type The message type of the Laravel Validation errors + */ + public function setLaravelValidationErrorType($type) + { + $this->laravelValidationErrorType = $type; + + return $this; + } + /** * Return available types of messages in container. * @@ -353,6 +374,23 @@ protected function addInstance(Message $message, $type, $flash = true, $format = } } + /** + * Add all the Laravel Validation erors to this collection. + * + * @param \Illuminate\Contracts\Validation\Validator $validator + * @param string $type Can be used to overwrite the Laravel + * Validation error type for this collection. + * @param boolean $flash + * @param string $format + */ + public function addLaravelValidationErrors(ValidatorContract $validator, $type = null, $flash = true, $format = null) { + if ($type === null) $type = $this->laravelValidationErrorType; + + foreach($validator->errors()->all() as $message) { + $this->add($type, $message, $flash, $format); + } + } + /** * Returns all messages for given type. * diff --git a/src/config/notification.php b/src/config/notification.php index c83bfe8..76783fa 100644 --- a/src/config/notification.php +++ b/src/config/notification.php @@ -42,6 +42,30 @@ */ 'types' => array(), + /* + |-------------------------------------------------------------------------- + | Default type for laravel validation errors + |-------------------------------------------------------------------------- + | + | This type will be used for new containers. + | Take note that the type specified here has to be specified in the + | types that can be used by the container as well + | + */ + 'default_laravel_validation_error_type' => 'error', + + /* + |-------------------------------------------------------------------------- + | Type for laravel validation errors for containers + |-------------------------------------------------------------------------- + | + | Specify the type for each container separately. + | Take note that the types specified here have to be specified in the + | types that can be used by the container as well + | + */ + 'laravel_validation_error_types' => array(), + /* |-------------------------------------------------------------------------- | Default message format From 7d0d67fdca556bc36491e3d17385c142751c257a Mon Sep 17 00:00:00 2001 From: Mitchell Klijs Date: Tue, 3 Jan 2017 11:20:38 +0100 Subject: [PATCH 3/5] Revert "Laravel Validation Errors" This reverts commit 0d2d4b3c4df94a1dac20ff8efb743b3220406c01. --- src/Krucas/Notification/NotificationsBag.php | 40 +------------------- src/config/notification.php | 24 ------------ 2 files changed, 1 insertion(+), 63 deletions(-) diff --git a/src/Krucas/Notification/NotificationsBag.php b/src/Krucas/Notification/NotificationsBag.php index a492675..d3b51fc 100644 --- a/src/Krucas/Notification/NotificationsBag.php +++ b/src/Krucas/Notification/NotificationsBag.php @@ -3,7 +3,6 @@ use Countable; use Illuminate\Contracts\Support\Arrayable; use Illuminate\Contracts\Support\Jsonable; -use Illuminate\Contracts\Validation\Validator as ValidatorContract; use Closure; class NotificationsBag implements Arrayable, Jsonable, Countable @@ -22,13 +21,6 @@ class NotificationsBag implements Arrayable, Jsonable, Countable */ protected $types = array(); - /** - * The Laravel Validation error type - * - * @var string - */ - protected $laravelValidationErrorType; - /** * Array of matcher for extracting types. * @@ -84,11 +76,10 @@ class NotificationsBag implements Arrayable, Jsonable, Countable * @param null $defaultFormat * @param array $formats */ - public function __construct($container, $types = array(), $laravelValidationErrorType = null, $defaultFormat = null, $formats = array()) + public function __construct($container, $types = array(), $defaultFormat = null, $formats = array()) { $this->container = $container; $this->addType($types); - $this->setLaravelValidationErrorType($laravelValidationErrorType); $this->setDefaultFormat($defaultFormat); $this->setFormats($formats); $this->notifications = new Collection(); @@ -131,18 +122,6 @@ public function addType($type) return $this; } - /** - * Set the Laravel Validation error type - * - * @param string $type The message type of the Laravel Validation errors - */ - public function setLaravelValidationErrorType($type) - { - $this->laravelValidationErrorType = $type; - - return $this; - } - /** * Return available types of messages in container. * @@ -374,23 +353,6 @@ protected function addInstance(Message $message, $type, $flash = true, $format = } } - /** - * Add all the Laravel Validation erors to this collection. - * - * @param \Illuminate\Contracts\Validation\Validator $validator - * @param string $type Can be used to overwrite the Laravel - * Validation error type for this collection. - * @param boolean $flash - * @param string $format - */ - public function addLaravelValidationErrors(ValidatorContract $validator, $type = null, $flash = true, $format = null) { - if ($type === null) $type = $this->laravelValidationErrorType; - - foreach($validator->errors()->all() as $message) { - $this->add($type, $message, $flash, $format); - } - } - /** * Returns all messages for given type. * diff --git a/src/config/notification.php b/src/config/notification.php index 76783fa..c83bfe8 100644 --- a/src/config/notification.php +++ b/src/config/notification.php @@ -42,30 +42,6 @@ */ 'types' => array(), - /* - |-------------------------------------------------------------------------- - | Default type for laravel validation errors - |-------------------------------------------------------------------------- - | - | This type will be used for new containers. - | Take note that the type specified here has to be specified in the - | types that can be used by the container as well - | - */ - 'default_laravel_validation_error_type' => 'error', - - /* - |-------------------------------------------------------------------------- - | Type for laravel validation errors for containers - |-------------------------------------------------------------------------- - | - | Specify the type for each container separately. - | Take note that the types specified here have to be specified in the - | types that can be used by the container as well - | - */ - 'laravel_validation_error_types' => array(), - /* |-------------------------------------------------------------------------- | Default message format From 34c59c14c416578b841167b493cf180a2f8b69d4 Mon Sep 17 00:00:00 2001 From: Mitchell Klijs Date: Tue, 3 Jan 2017 11:24:53 +0100 Subject: [PATCH 4/5] Laravel Validation Errors --- src/Krucas/Notification/NotificationsBag.php | 41 +++++++++++++++++++- src/config/notification.php | 23 +++++++++++ 2 files changed, 63 insertions(+), 1 deletion(-) diff --git a/src/Krucas/Notification/NotificationsBag.php b/src/Krucas/Notification/NotificationsBag.php index d3b51fc..750e6cc 100644 --- a/src/Krucas/Notification/NotificationsBag.php +++ b/src/Krucas/Notification/NotificationsBag.php @@ -3,6 +3,7 @@ use Countable; use Illuminate\Contracts\Support\Arrayable; use Illuminate\Contracts\Support\Jsonable; +use Illuminate\Contracts\Validation\Validator as ValidatorContract; use Closure; class NotificationsBag implements Arrayable, Jsonable, Countable @@ -21,6 +22,13 @@ class NotificationsBag implements Arrayable, Jsonable, Countable */ protected $types = array(); + /* + * The Laravel Validation error type + * + * @var string + */ + protected $laravelValidationErrorType; + /** * Array of matcher for extracting types. * @@ -73,13 +81,15 @@ class NotificationsBag implements Arrayable, Jsonable, Countable * * @param $container * @param array $types + * @param null $laravelValidationErrorType * @param null $defaultFormat * @param array $formats */ - public function __construct($container, $types = array(), $defaultFormat = null, $formats = array()) + public function __construct($container, $types = array(), $laravelValidationErrorType = null, $defaultFormat = null, $formats = array()) { $this->container = $container; $this->addType($types); + $this->setLaravelValidationErrorType($laravelValidationErrorType); $this->setDefaultFormat($defaultFormat); $this->setFormats($formats); $this->notifications = new Collection(); @@ -122,6 +132,18 @@ public function addType($type) return $this; } + /** + * Set the Laravel Validation error type + * + * @param string $type The message type of the Laravel Validation errors + */ + public function setLaravelValidationErrorType($type) + { + $this->laravelValidationErrorType = $type; + + return $this; + } + /** * Return available types of messages in container. * @@ -353,6 +375,23 @@ protected function addInstance(Message $message, $type, $flash = true, $format = } } + /** + * Add all the Laravel Validation erors to this collection. + * + * @param \Illuminate\Contracts\Validation\Validator $validator + * @param string $type Can be used to overwrite the Laravel + * Validation error type for this collection. + * @param boolean $flash + * @param string $format + */ + public function addLaravelValidationErrors(ValidatorContract $validator, $type = null, $flash = true, $format = null) { + if ($type === null) $type = $this->laravelValidationErrorType; + + foreach($validator->errors()->all() as $message) { + $this->add($type, $message, $flash, $format); + } + } + /** * Returns all messages for given type. * diff --git a/src/config/notification.php b/src/config/notification.php index c83bfe8..52b0950 100644 --- a/src/config/notification.php +++ b/src/config/notification.php @@ -42,6 +42,29 @@ */ 'types' => array(), + /* + | Default type for laravel validation errors + |-------------------------------------------------------------------------- + | + | This type will be used for new containers. + | Take note that the type specified here has to be specified in the + | types that can be used by the container as well + | + */ + 'default_laravel_validation_error_type' => 'error', + + /* + |-------------------------------------------------------------------------- + | Type for laravel validation errors for containers + |-------------------------------------------------------------------------- + | + | Specify the type for each container separately. + | Take note that the types specified here have to be specified in the + | types that can be used by the container as well + | + */ + 'laravel_validation_error_types' => array(), + /* |-------------------------------------------------------------------------- | Default message format From a5262b908219c3f58cf96fcfe5ce1588ad6bc929 Mon Sep 17 00:00:00 2001 From: Mitchell Klijs Date: Mon, 28 Aug 2017 20:02:04 +0200 Subject: [PATCH 5/5] New version --- README.md | 8 +++++ src/Krucas/Notification/Notification.php | 22 ++++++------ .../NotificationServiceProvider.php | 1 - src/Krucas/Notification/NotificationsBag.php | 36 +++++++++++++------ src/config/notification.php | 14 +------- tests/NotificationTest.php | 5 ++- 6 files changed, 49 insertions(+), 37 deletions(-) diff --git a/README.md b/README.md index 38438e7..7b65165 100644 --- a/README.md +++ b/README.md @@ -70,6 +70,14 @@ Adding message to default container. \Krucas\Notification\Facades\Notification::warning('Warning message'); ``` +### Add errors from laravel validation +Adding errors from the Laravel validator to the default container. +```php +$validator = // Your validation in a controller or custom validation object + +\Krucas\Notification\Facades\Notification::addLaravelValidationErrors($validator); +``` + ### Containers Containers allows you to set up different containers for different placeholders. diff --git a/src/Krucas/Notification/Notification.php b/src/Krucas/Notification/Notification.php index 452d484..01735e1 100644 --- a/src/Krucas/Notification/Notification.php +++ b/src/Krucas/Notification/Notification.php @@ -38,14 +38,14 @@ class Notification * * @var string */ - protected $defaultLaravelValidationErrorType; + protected $laravelValidationErrorType; /** - * Laravel Validation error types for defined containers. + * Laravel Validation error types for defined containers * - * @var array + * @var string */ - protected $laravelValidationErrorType = []; + protected $laravelValidationErrorTypes; /** * Default format for each container type. @@ -88,6 +88,7 @@ class Notification * @param string $defaultContainer * @param array $defaultTypes * @param array $types + * @param string $laravelValidationErrorType * @param string $defaultFormat * @param array $format * @param array $defaultFormats @@ -97,7 +98,6 @@ public function __construct( $defaultContainer, $defaultTypes, $types, - $defaultLaravelValidationErrorType, $laravelValidationErrorType, $defaultFormat, $format, @@ -107,7 +107,6 @@ public function __construct( $this->defaultContainer = $defaultContainer; $this->defaultTypes = $defaultTypes; $this->types = $types; - $this->defaultLaravelValidationErrorType = $defaultLaravelValidationErrorType; $this->laravelValidationErrorType = $laravelValidationErrorType; $this->defaultFormat = $defaultFormat; $this->format = $format; @@ -163,7 +162,7 @@ public function getContainerTypes($container) */ public function setContainerLaravelValidationErrorType($container, $type) { - $this->laravelValidationErrorType[$container] = $type; + $this->laravelValidationErrorTypes[$container] = $type; return $this; } @@ -172,15 +171,15 @@ public function setContainerLaravelValidationErrorType($container, $type) * Return Laravel Validation error type for a container. * * @param $container - * @return array + * @return string */ public function getContainerLaravelValidationErrorType($container) { - if (isset($this->laravelValidationErrorType[$container])) { - return $this->laravelValidationErrorType[$container]; + if (isset($this->laravelValidationErrorTypes[$container])) { + return $this->laravelValidationErrorTypes[$container]; } - return $this->defaultLaravelValidationErrorType; + return $this->laravelValidationErrorType; } /** @@ -246,6 +245,7 @@ public function getContainerFormats($container) * * @param string $container * @param array $types + * @param string $laravelValidationErrorType * @param null $defaultFormat * @param array $formats * @return \Krucas\Notification\Notification diff --git a/src/Krucas/Notification/NotificationServiceProvider.php b/src/Krucas/Notification/NotificationServiceProvider.php index de9c974..00add55 100644 --- a/src/Krucas/Notification/NotificationServiceProvider.php +++ b/src/Krucas/Notification/NotificationServiceProvider.php @@ -54,7 +54,6 @@ public function register() $config->get('notification.default_container'), $config->get('notification.default_types'), $config->get('notification.types'), - $config->get('notification.default_laravel_validation_error_type'), $config->get('notification.laravel_validation_error_type'), $config->get('notification.default_format'), $config->get('notification.format'), diff --git a/src/Krucas/Notification/NotificationsBag.php b/src/Krucas/Notification/NotificationsBag.php index fea450e..1ea0958 100644 --- a/src/Krucas/Notification/NotificationsBag.php +++ b/src/Krucas/Notification/NotificationsBag.php @@ -1,10 +1,10 @@ laravelValidationErrorType; + } + /** * Set the Laravel Validation error type - * + * * @param string $type The message type of the Laravel Validation errors + * @return \Krucas\Notification\NotificationsBag */ public function setLaravelValidationErrorType($type) { @@ -377,17 +388,20 @@ protected function addInstance(Message $message, $type, $flash = true, $format = /** * Add all the Laravel Validation erors to this collection. - * + * * @param \Illuminate\Contracts\Validation\Validator $validator - * @param string $type Can be used to overwrite the Laravel - * Validation error type for this collection. - * @param boolean $flash - * @param string $format + * @param string $type Can be used to overwrite the Laravel + * Validation error type + * @param boolean $flash + * @param string $format */ - public function addLaravelValidationErrors(ValidatorContract $validator, $type = null, $flash = true, $format = null) { - if ($type === null) $type = $this->laravelValidationErrorType; + public function addLaravelValidationErrors(Validator $validator, $type = null, $flash = true, $format = null) + { + if ($type === null) { + $type = $this->laravelValidationErrorType; + } - foreach($validator->errors()->all() as $message) { + foreach ($validator->errors()->all() as $message) { $this->add($type, $message, $flash, $format); } } diff --git a/src/config/notification.php b/src/config/notification.php index 52b0950..8934c0a 100644 --- a/src/config/notification.php +++ b/src/config/notification.php @@ -51,19 +51,7 @@ | types that can be used by the container as well | */ - 'default_laravel_validation_error_type' => 'error', - - /* - |-------------------------------------------------------------------------- - | Type for laravel validation errors for containers - |-------------------------------------------------------------------------- - | - | Specify the type for each container separately. - | Take note that the types specified here have to be specified in the - | types that can be used by the container as well - | - */ - 'laravel_validation_error_types' => array(), + 'laravel_validation_error_type' => 'error', /* |-------------------------------------------------------------------------- diff --git a/tests/NotificationTest.php b/tests/NotificationTest.php index 81e6483..c405399 100644 --- a/tests/NotificationTest.php +++ b/tests/NotificationTest.php @@ -225,6 +225,7 @@ public function testCreateNewContainerWithDefaults() 'default', ['info', 'warning', 'success', 'error'], [], + 'info', ':type :message', [], [], @@ -236,6 +237,7 @@ public function testCreateNewContainerWithDefaults() $this->assertEquals('test', $container->getName()); $this->assertEquals(':type :message', $container->getDefaultFormat()); $this->assertEquals(['info', 'warning', 'success', 'error'], $container->getTypes()); + $this->assertEquals('info', $container->getLaravelValidationErrorType()); } public function testCreateNewContainerFromDefined() @@ -246,6 +248,7 @@ public function testCreateNewContainerFromDefined() [ 'test' => ['info'] ], + null, ':type :message', [ 'test' => ':message', @@ -268,7 +271,7 @@ public function testCreateNewContainerFromDefined() protected function getNotification() { - return new \Krucas\Notification\Notification('default', [], [], null, [], [], []); + return new \Krucas\Notification\Notification('default', [], [], null,null, null, [], []); } protected function getMessage()