From a8a1cfdc123bf1efd9a7a63db4442db694f14f50 Mon Sep 17 00:00:00 2001 From: Paul Klimov Date: Wed, 9 Sep 2020 16:09:39 +0300 Subject: [PATCH] #13: Fixed validation for the item ID containing dot ('.') --- CHANGELOG.md | 7 +++++++ README.md | 4 ++-- UPGRADE.md | 6 ++++++ composer.json | 4 ++-- src/PersistentRepository.php | 17 +++++------------ 5 files changed, 22 insertions(+), 16 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 82825ca..aa0d190 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,13 @@ Laravel Persistent Configuration Repository =========================================== +1.3.0 Under Development +----------------------- + +- Bug #13: Fixed validation for the item ID containing dot ('.') (klimov-paul) +- Enh: Added support for "illuminate/support" 8.0 (klimov-paul) + + 1.2.0, March 11, 2020 --------------------- diff --git a/README.md b/README.md index 0891f43..414a03f 100644 --- a/README.md +++ b/README.md @@ -496,8 +496,8 @@ $validatedData = $config->validate($request->all()); // throws \Illuminate\Valid You can also use `\Illuminatech\Config\PersistentRepository::makeValidator()` method to create a validator instance for manual processing. **Heads up!** Watch for usage dot symbols ('.') inside the input in case you do not use `\Illuminatech\Config\PersistentRepository::validate()` method. -By default Laravel considers dots in validation rules as array nested keys separator. You should either replace them -by '->' string or manually define `\Illuminatech\Config\Item::$id` in the way it does not contain a dot. +By default Laravel considers dots in validation rules as array nested keys separator. You should either prefix them +with backslash ('\') or manually define `\Illuminatech\Config\Item::$id` in the way it does not contain a dot. ### Creating configuration web interface diff --git a/UPGRADE.md b/UPGRADE.md index b01a02c..20ae5af 100644 --- a/UPGRADE.md +++ b/UPGRADE.md @@ -8,6 +8,12 @@ if you want to upgrade from version A to version C and there is version B between A and C, you need to following the instructions for both A and B. +Upgrade from 1.2.0 +------------------ + +* "illuminate/support" package requirements were raised to 7.26.0. Make sure to upgrade your code accordingly. + + Upgrade from 1.1.1 ------------------ diff --git a/composer.json b/composer.json index 4f504f5..35368fd 100644 --- a/composer.json +++ b/composer.json @@ -15,7 +15,7 @@ } ], "require": { - "illuminate/support": "^6.0 || ^7.0" + "illuminate/support": "^7.26.0 || ^8.0" }, "require-dev": { "illuminate/database": "*", @@ -24,7 +24,7 @@ "illuminate/encryption": "*", "illuminate/validation": "*", "psr/log": "^1.0", - "phpunit/phpunit": "^7.5 || ^8.0" + "phpunit/phpunit": "^8.0 || ^9.3" }, "autoload": { "psr-4": { diff --git a/src/PersistentRepository.php b/src/PersistentRepository.php index 2f711c0..3b9e320 100644 --- a/src/PersistentRepository.php +++ b/src/PersistentRepository.php @@ -362,7 +362,7 @@ public function makeValidator(array $values): Validator { $rules = []; foreach ($this->getItems() as $item) { - $inputName = str_replace('.', '->', $item->id); + $inputName = str_replace('.', '\.', $item->id); $rules[$inputName] = $item->rules; } @@ -386,11 +386,10 @@ public function validate(array $values): array if ($validator->fails()) { $errors = []; foreach ($validator->errors()->getMessages() as $key => $messages) { - $itemId = str_replace('->', '.', $key); - $errors[$itemId] = []; + $errors[$key] = []; foreach ($messages as $message) { - $itemLabel = $items[$itemId]->label; - $errors[$itemId][] = str_replace( + $itemLabel = $items[$key]->label; + $errors[$key][] = str_replace( [ $key, Str::ucfirst($key), @@ -409,13 +408,7 @@ public function validate(array $values): array throw ValidationException::withMessages($errors); } - $itemValues = []; - foreach ($validator->validated() as $key => $value) { - $itemId = str_replace('->', '.', $key); - $itemValues[$itemId] = $value; - } - - return $itemValues; + return $validator->validated(); } /**