From 321367b974268f23359ab2ad92284c559c262998 Mon Sep 17 00:00:00 2001 From: Ivan Nieto Vivanco Date: Wed, 22 Jun 2022 21:06:07 -0500 Subject: [PATCH 1/4] Adds validation to custom fields' default values --- .../Controllers/AssetModelsController.php | 25 +++++++++++++++++-- 1 file changed, 23 insertions(+), 2 deletions(-) diff --git a/app/Http/Controllers/AssetModelsController.php b/app/Http/Controllers/AssetModelsController.php index 9a7c1407bc..29d7ceee95 100755 --- a/app/Http/Controllers/AssetModelsController.php +++ b/app/Http/Controllers/AssetModelsController.php @@ -8,6 +8,7 @@ use App\Models\AssetModel; use Illuminate\Support\Facades\Auth; use Illuminate\Support\Facades\Input; use Illuminate\Support\Facades\View; +use Illuminate\Support\Facades\Validator; use Redirect; use Request; use Storage; @@ -90,7 +91,9 @@ class AssetModelsController extends Controller // Was it created? if ($model->save()) { if ($this->shouldAddDefaultValues($request->input())) { - $this->assignCustomFieldsDefaultValues($model, $request->input('default_values')); + if (!$this->assignCustomFieldsDefaultValues($model, $request->input('default_values'))){ + return redirect()->back()->withInput()->withErrors($model->getErrors()); + } } // Redirect to the new model page @@ -163,7 +166,9 @@ class AssetModelsController extends Controller $model->fieldset_id = $request->input('custom_fieldset'); if ($this->shouldAddDefaultValues($request->input())) { - $this->assignCustomFieldsDefaultValues($model, $request->input('default_values')); + if (!$this->assignCustomFieldsDefaultValues($model, $request->input('default_values'))){ + return redirect()->back()->withInput()->withErrors("Error"); + } } } @@ -451,6 +456,21 @@ class AssetModelsController extends Controller */ private function assignCustomFieldsDefaultValues(AssetModel $model, array $defaultValues) { + $data = array(); + foreach ($defaultValues as $customFieldId => $defaultValue) { + $customField = \App\Models\CustomField::find($customFieldId); + + $data[$customField->db_column] = $defaultValue; + } + + $rules = $model->fieldset->validation_rules(); + + $validator = Validator::make($data, $rules); + + if($validator->fails()){ + return false; + } + foreach ($defaultValues as $customFieldId => $defaultValue) { if(is_array($defaultValue)){ $model->defaultValues()->attach($customFieldId, ['default_value' => implode(', ', $defaultValue)]); @@ -458,6 +478,7 @@ class AssetModelsController extends Controller $model->defaultValues()->attach($customFieldId, ['default_value' => $defaultValue]); } } + return true; } /** From afdf93ca63b7c5ab375bf00dcd6b1fe43a7e22f7 Mon Sep 17 00:00:00 2001 From: Ivan Nieto Vivanco Date: Wed, 22 Jun 2022 21:37:11 -0500 Subject: [PATCH 2/4] Adds controls for custom fields of type date in the default value form --- ...custom-field-set-default-values-for-model.blade.php | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/resources/views/livewire/custom-field-set-default-values-for-model.blade.php b/resources/views/livewire/custom-field-set-default-values-for-model.blade.php index c1a8373f47..59aab3aee6 100644 --- a/resources/views/livewire/custom-field-set-default-values-for-model.blade.php +++ b/resources/views/livewire/custom-field-set-default-values-for-model.blade.php @@ -22,8 +22,14 @@
- - @if ($field->element == "text") + @if ($field->format == "DATE") +
+
+ + +
+
+ @elseif ($field->element == "text") @elseif($field->element == "textarea")
From 7e10abe6052c1b4ffa8a52f6262dda175bb9779b Mon Sep 17 00:00:00 2001 From: Ivan Nieto Vivanco Date: Wed, 22 Jun 2022 21:47:08 -0500 Subject: [PATCH 3/4] Fix the date control in custom fields' default values form to show the selected date --- .../custom-field-set-default-values-for-model.blade.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/resources/views/livewire/custom-field-set-default-values-for-model.blade.php b/resources/views/livewire/custom-field-set-default-values-for-model.blade.php index 59aab3aee6..220956115c 100644 --- a/resources/views/livewire/custom-field-set-default-values-for-model.blade.php +++ b/resources/views/livewire/custom-field-set-default-values-for-model.blade.php @@ -25,7 +25,7 @@ @if ($field->format == "DATE")
- +
From 7976401aa295dd2989d98c6a90d1f99ab722e186 Mon Sep 17 00:00:00 2001 From: Ivan Nieto Vivanco Date: Wed, 22 Jun 2022 22:17:05 -0500 Subject: [PATCH 4/4] Add error message when the default customfield values can't be validated --- app/Http/Controllers/AssetModelsController.php | 4 ++-- resources/lang/en/admin/custom_fields/message.php | 6 ++++++ 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/app/Http/Controllers/AssetModelsController.php b/app/Http/Controllers/AssetModelsController.php index 29d7ceee95..b74be17789 100755 --- a/app/Http/Controllers/AssetModelsController.php +++ b/app/Http/Controllers/AssetModelsController.php @@ -92,7 +92,7 @@ class AssetModelsController extends Controller if ($model->save()) { if ($this->shouldAddDefaultValues($request->input())) { if (!$this->assignCustomFieldsDefaultValues($model, $request->input('default_values'))){ - return redirect()->back()->withInput()->withErrors($model->getErrors()); + return redirect()->back()->withInput()->with('error', trans('admin/custom_fields/message.fieldset_default_value.error')); } } @@ -167,7 +167,7 @@ class AssetModelsController extends Controller if ($this->shouldAddDefaultValues($request->input())) { if (!$this->assignCustomFieldsDefaultValues($model, $request->input('default_values'))){ - return redirect()->back()->withInput()->withErrors("Error"); + return redirect()->back()->withInput()->with('error', trans('admin/custom_fields/message.fieldset_default_value.error')); } } } diff --git a/resources/lang/en/admin/custom_fields/message.php b/resources/lang/en/admin/custom_fields/message.php index ed66a6ba16..43ba821821 100644 --- a/resources/lang/en/admin/custom_fields/message.php +++ b/resources/lang/en/admin/custom_fields/message.php @@ -49,6 +49,12 @@ return array( ), + 'fieldset_default_value' => array( + + 'error' => 'Error validating default fieldset values.', + + ), +