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 8e1e4ab..01735e1 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 $laravelValidationErrorType; + + /** + * Laravel Validation error types for defined containers + * + * @var string + */ + protected $laravelValidationErrorTypes; + /** * Default format for each container type. * @@ -74,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 @@ -83,6 +98,7 @@ public function __construct( $defaultContainer, $defaultTypes, $types, + $laravelValidationErrorType, $defaultFormat, $format, $defaultFormats, @@ -91,6 +107,7 @@ public function __construct( $this->defaultContainer = $defaultContainer; $this->defaultTypes = $defaultTypes; $this->types = $types; + $this->laravelValidationErrorType = $laravelValidationErrorType; $this->defaultFormat = $defaultFormat; $this->format = $format; $this->defaultFormats = $defaultFormats; @@ -136,6 +153,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->laravelValidationErrorTypes[$container] = $type; + + return $this; + } + + /** + * Return Laravel Validation error type for a container. + * + * @param $container + * @return string + */ + public function getContainerLaravelValidationErrorType($container) + { + if (isset($this->laravelValidationErrorTypes[$container])) { + return $this->laravelValidationErrorTypes[$container]; + } + + return $this->laravelValidationErrorType; + } + /** * Set format for a container. * @@ -199,17 +245,18 @@ public function getContainerFormats($container) * * @param string $container * @param array $types + * @param string $laravelValidationErrorType * @param null $defaultFormat * @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 17b6991..00add55 100644 --- a/src/Krucas/Notification/NotificationServiceProvider.php +++ b/src/Krucas/Notification/NotificationServiceProvider.php @@ -54,6 +54,7 @@ public function register() $config->get('notification.default_container'), $config->get('notification.default_types'), $config->get('notification.types'), + $config->get('notification.laravel_validation_error_type'), $config->get('notification.default_format'), $config->get('notification.format'), $config->get('notification.default_formats'), diff --git a/src/Krucas/Notification/NotificationsBag.php b/src/Krucas/Notification/NotificationsBag.php index afd1d8d..1ea0958 100644 --- a/src/Krucas/Notification/NotificationsBag.php +++ b/src/Krucas/Notification/NotificationsBag.php @@ -1,9 +1,10 @@ container = $container; $this->addType($types); + $this->setLaravelValidationErrorType($laravelValidationErrorType); $this->setDefaultFormat($defaultFormat); $this->setFormats($formats); $this->notifications = new Collection(); @@ -122,6 +132,29 @@ public function addType($type) return $this; } + /** + * Get the Laravel Validation error type + * + * @return \Krucas\Notification\NotificationsBag + */ + public function getLaravelValidationErrorType() + { + return $this->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) + { + $this->laravelValidationErrorType = $type; + + return $this; + } + /** * Return available types of messages in container. * @@ -353,6 +386,26 @@ 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 + * @param boolean $flash + * @param string $format + */ + public function addLaravelValidationErrors(Validator $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..8934c0a 100644 --- a/src/config/notification.php +++ b/src/config/notification.php @@ -42,6 +42,17 @@ */ '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 + | + */ + 'laravel_validation_error_type' => 'error', + /* |-------------------------------------------------------------------------- | Default message format 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()