From 9bb349d34be0933bdbcc4c3db7df347e21197e3a Mon Sep 17 00:00:00 2001 From: snipe Date: Tue, 8 Apr 2025 02:00:37 +0100 Subject: [PATCH] Try to get the asset from the route if there is RMB Signed-off-by: snipe --- .../Traits/MayContainCustomFields.php | 25 +++++++++++++------ 1 file changed, 17 insertions(+), 8 deletions(-) diff --git a/app/Http/Requests/Traits/MayContainCustomFields.php b/app/Http/Requests/Traits/MayContainCustomFields.php index bbdf62893d..ed0c96dace 100644 --- a/app/Http/Requests/Traits/MayContainCustomFields.php +++ b/app/Http/Requests/Traits/MayContainCustomFields.php @@ -10,19 +10,28 @@ trait MayContainCustomFields // this gets called automatically on a form request public function withValidator($validator) { - // find the model - if ($this->method() == 'POST') { - $asset_model = AssetModel::find($this->model_id); - } - if ($this->method() == 'PATCH' || $this->method() == 'PUT') { - $asset_model = $this->asset->model; - } + + + // For auditing and some other non-standard things where $this is only the form submission and may not have the asset info + if ((request()->route('asset') && (request()->route('asset')->model_id))) { + $asset_model = AssetModel::find(request()->route('asset')->model_id); += } else { + // find the model + if ($this->method() == 'POST') { + $asset_model = AssetModel::find($this->model_id); + } + if ($this->method() == 'PATCH' || $this->method() == 'PUT') { + $asset_model = $this->asset->model; + } + } + // collect the custom fields in the request $validator->after(function ($validator) use ($asset_model) { $request_fields = $this->collect()->keys()->filter(function ($attributes) { return str_starts_with($attributes, '_snipeit_'); }); - // if there are custom fields, find the one's that don't exist on the model's fieldset and add an error to the validator's error bag + + // if there are custom fields, find the ones that don't exist on the model's fieldset and add an error to the validator's error bag if (count($request_fields) > 0 && $validator->errors()->isEmpty()) { $request_fields->diff($asset_model?->fieldset?->fields?->pluck('db_column')) ->each(function ($request_field_name) use ($request_fields, $validator) {