3
0
mirror of https://github.com/snipe/snipe-it.git synced 2026-02-04 12:55:23 +00:00

got validation to redirect back to form and display

This commit is contained in:
Godfrey M
2025-06-24 12:42:07 -07:00
parent 6015aeddee
commit 942de9dce5
4 changed files with 54 additions and 7 deletions

View File

@ -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';

View File

@ -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'));
}
}

View File

@ -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>

View File

@ -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',