Skip to content

A free open source IT asset/license management system

got validation to redirect back to form and display

Godfrey M942de9dce5dc
diff --git a/app/Exceptions/Handler.php b/app/Exceptions/Handler.php
index f544180750..7201b53f5d 100644
--- a/app/Exceptions/Handler.php
+++ b/app/Exceptions/Handler.php
@@ -125,8 +125,17 @@ class Handler extends ExceptionHandler
// This is traaaaash but it handles models that are not found while using route model binding :(
// The only alternative is to set that at *each* route, which is crazypants
if ($e instanceof \Illuminate\Database\Eloquent\ModelNotFoundException) {
+ $ids = method_exists($e, 'getIds') ? $e->getIds() : [];
- // This gets the MVC model name from the exception and formats in a way that's less fugly
+ if (in_array('bulkedit', $ids, true)) {
+
+ return redirect()
+ ->route('hardware.bulkedit')
+ ->with('bulk_asset_errors', session()->pull('bulk_asset_errors'))
+ ->withInput();
+ }
+
+ // This gets the MVC model name from the exception and formats in a way that's less fugly
$model_name = strtolower(implode(" ", preg_split('/(?=[A-Z])/', last(explode('\\', $e->getModel())))));
$route = str_plural(strtolower(last(explode('\\', $e->getModel())))).'.index';
diff --git a/app/Http/Controllers/Assets/BulkAssetsController.php b/app/Http/Controllers/Assets/BulkAssetsController.php
index f875bad56a..39dd3c79ab 100644
--- a/app/Http/Controllers/Assets/BulkAssetsController.php
+++ b/app/Http/Controllers/Assets/BulkAssetsController.php
@@ -221,11 +221,11 @@ class BulkAssetsController extends Controller
$error_array = array();
// Get the back url from the session and then destroy the session
- $bulk_back_url = route('hardware.index');
- if ($request->session()->has('bulk_back_url')) {
- $bulk_back_url = $request->session()->pull('bulk_back_url');
- }
+ $bulk_back_url = $request->session()->pull('bulk_back_url', url()->previous());
+// if ($request->session()->has('bulk_back_url')) {
+// $bulk_back_url = $request->session()->pull('bulk_back_url');
+// }
$custom_field_columns = CustomField::all()->pluck('db_column')->toArray();
@@ -540,7 +540,13 @@ class BulkAssetsController extends Controller
} // end asset foreach
if ($has_errors > 0) {
- return redirect($bulk_back_url)->with('bulk_asset_errors', $error_array);
+ session()->put('bulkedit_ids', $request->input('ids'));
+ session()->put('bulk_asset_errors',$error_array);
+
+
+ return redirect(url()->previous())
+ ->with('bulk_asset_errors', $error_array)
+ ->withInput();
}
return redirect($bulk_back_url)->with('success', trans('admin/hardware/message.update.success'));
@@ -732,4 +738,35 @@ class BulkAssetsController extends Controller
}
return false;
}
+
+ public function bulkEditForm(Request $request): View|RedirectResponse
+ {
+ $this->authorize('update', Asset::class);
+
+ $asset_ids = session()->pull('bulkedit_ids', []);
+
+ if (empty($asset_ids)) {
+ return redirect()->route('hardware.index')->with('error', trans('admin/hardware/message.update.no_assets_selected'));
+ }
+
+ $assets = Asset::with('model')->withTrashed()->whereIn('id', $asset_ids)->get();
+
+ if ($assets->isEmpty()) {
+ return redirect()->route('hardware.index')->with('error', trans('admin/hardware/message.update.assets_do_not_exist_or_are_invalid'));
+ }
+
+ $models = $assets->unique('model_id');
+ $modelNames = [];
+ foreach ($models as $model) {
+ $modelNames[] = $model->model->name;
+ }
+
+ return view('hardware/bulk')
+ ->with('assets', $asset_ids)
+ ->with('statuslabel_list', Helper::statusLabelList())
+ ->with('models', $models->pluck(['model']))
+ ->with('modelNames', $modelNames)
+ ->with('bulk_asset_errors', session('bulk_asset_errors'));
+ }
+
}
diff --git a/resources/views/models/custom_fields_form_bulk_edit.blade.php b/resources/views/models/custom_fields_form_bulk_edit.blade.php
index 194c033dd9..8076c29822 100644
--- a/resources/views/models/custom_fields_form_bulk_edit.blade.php
+++ b/resources/views/models/custom_fields_form_bulk_edit.blade.php
@@ -143,7 +143,7 @@
<div class="col-md-8 col-md-offset-3" style="padding-bottom: 10px;">
<label class="form-control">
- <input type="checkbox" name="{{ 'null'.$field->db_column_name() }}" value="1">
+ <input type="checkbox" name="{{ 'null'.$field->db_column_name() }}" value="1" {{ old('null'.$field->db_column_name()) ? 'checked' : '' }}>
{{ trans_choice('general.set_to_null', count($assets),['selection_count' => count($assets)]) }}
</label>
</div>
diff --git a/routes/web/hardware.php b/routes/web/hardware.php
index 21f4f40d41..9c15a31f62 100644
--- a/routes/web/hardware.php
+++ b/routes/web/hardware.php
@@ -152,6 +152,7 @@ Route::group(
Route::delete('{asset}/showfile/{fileId}/delete',
[AssetFilesController::class, 'destroy']
)->name('delete/assetfile')->withTrashed();
+ Route::get('hardware/bulkedit', [BulkAssetsController::class, 'bulkEditForm'])->name('hardware.bulkedit');
Route::post(
'bulkedit',