From 30d0125cbda0432aaeb908d16861c1d1f828ce97 Mon Sep 17 00:00:00 2001 From: Ivan Nieto Vivanco Date: Wed, 24 Aug 2022 16:18:11 -0500 Subject: [PATCH] Remove the rule 'required' for default values, if custom field is obligatory --- app/Http/Controllers/AssetModelsController.php | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/app/Http/Controllers/AssetModelsController.php b/app/Http/Controllers/AssetModelsController.php index b74be17789..2eae579adb 100755 --- a/app/Http/Controllers/AssetModelsController.php +++ b/app/Http/Controllers/AssetModelsController.php @@ -13,6 +13,7 @@ use Redirect; use Request; use Storage; use Symfony\Component\HttpFoundation\JsonResponse; +use function JmesPath\search; /** * This class controls all actions related to asset models for @@ -463,7 +464,18 @@ class AssetModelsController extends Controller $data[$customField->db_column] = $defaultValue; } - $rules = $model->fieldset->validation_rules(); + $fieldsets = $model->fieldset->validation_rules(); + $rules = array(); + + foreach ($fieldsets as $fieldset => $validation){ + // If the field is marked as required, eliminate the rule so it doesn't interfere with the default values + // (we are at model level, the rule still applies when creating a new asset using this model) + $index = array_search('required', $validation); + if ($index !== false){ + unset($validation[$index]); + } + $rules[$fieldset] = $validation; + } $validator = Validator::make($data, $rules);