From f2e666d669189039c4895be315e801c625f15dac Mon Sep 17 00:00:00 2001 From: Philippe Lonchampt Date: Tue, 3 Nov 2015 16:01:22 +0100 Subject: [PATCH] Fix validation bug with ~ --- .../views/cms/partials/formField.blade.php | 2 +- src/Dvlpp/Sharp/Http/EntityController.php | 19 +++++++++++++++++++ src/Dvlpp/Sharp/Validation/SharpValidator.php | 4 +++- src/helpers/sharp_internal.php | 6 ++++++ 4 files changed, 29 insertions(+), 2 deletions(-) diff --git a/resources/views/cms/partials/formField.blade.php b/resources/views/cms/partials/formField.blade.php index b205ca5..a66f713 100644 --- a/resources/views/cms/partials/formField.blade.php +++ b/resources/views/cms/partials/formField.blade.php @@ -1,6 +1,6 @@ @if(!in_array($field->type, ["hidden", "javascript"])) -
conditional_display ? 'data-conditional_display='.$field->conditional_display : '' }} > diff --git a/src/Dvlpp/Sharp/Http/EntityController.php b/src/Dvlpp/Sharp/Http/EntityController.php index 3503b64..1d23270 100644 --- a/src/Dvlpp/Sharp/Http/EntityController.php +++ b/src/Dvlpp/Sharp/Http/EntityController.php @@ -4,6 +4,7 @@ use Dvlpp\Sharp\Exceptions\InvalidStateException; use Dvlpp\Sharp\Repositories\SharpCmsRepository; +use Illuminate\Contracts\Support\MessageBag; use Illuminate\Http\Request; use Dvlpp\Sharp\Config\SharpConfig; use Illuminate\Contracts\Auth\Access\Gate; @@ -344,6 +345,8 @@ private function save($categoryName, $entityName, Request $request, $id) ], 200); } catch (ValidationException $e) { + $this->formatValidationErrors($e->getErrors()); + return response()->json($e->getErrors(), 422); } } @@ -385,4 +388,20 @@ private function fireEvent($entityConfig, $eventName, $params) } } + /** + * Format validation errors, handling ~ special case. + * + * @param MessageBag $validationErrors + */ + private function formatValidationErrors(MessageBag $validationErrors) + { + $errors = []; + foreach($validationErrors->keys() as $key) { + $errors[str_replace("~", "-", $key)] = $validationErrors->get($key); + } + if(count($errors)) { + $validationErrors->merge($errors); + } + } + } \ No newline at end of file diff --git a/src/Dvlpp/Sharp/Validation/SharpValidator.php b/src/Dvlpp/Sharp/Validation/SharpValidator.php index e566166..4395464 100644 --- a/src/Dvlpp/Sharp/Validation/SharpValidator.php +++ b/src/Dvlpp/Sharp/Validation/SharpValidator.php @@ -1,4 +1,6 @@ -getDriver()->getAdapter()->getPathPrefix(); return $storagePath . $relativePath; +} + +function key_name_for_form_field($key) +{ + $key = str_replace("~", "-", $key); + return "sf-$key"; } \ No newline at end of file