diff --git a/app/Http/Controllers/LocationsController.php b/app/Http/Controllers/LocationsController.php index b78ba7513f..03e1a26e28 100755 --- a/app/Http/Controllers/LocationsController.php +++ b/app/Http/Controllers/LocationsController.php @@ -67,7 +67,6 @@ class LocationsController extends Controller { $this->authorize('create', Location::class); $location = new Location(); - $location->id = null; // This is required to make Laravels different validation work, it errors if the parameter doesn't exist (maybe a bug)? $location->name = $request->input('name'); $location->parent_id = $request->input('parent_id', null); $location->currency = $request->input('currency', '$'); @@ -132,7 +131,6 @@ class LocationsController extends Controller return redirect()->route('locations.index')->with('error', trans('admin/locations/message.does_not_exist')); } - // Update the location data $location->name = $request->input('name'); $location->parent_id = $request->input('parent_id', null); diff --git a/app/Models/Location.php b/app/Models/Location.php index fd7522c241..dc8aa6a043 100755 --- a/app/Models/Location.php +++ b/app/Models/Location.php @@ -28,7 +28,7 @@ class Location extends SnipeModel 'address2' => 'max:80|nullable', 'zip' => 'min:3|max:10|nullable', 'manager_id' => 'exists:users,id|nullable', - 'parent_id' => 'nullable|exists:locations,id|different:id', + 'parent_id' => 'non_circular:locations,id' ); protected $casts = [ diff --git a/app/Providers/ValidationServiceProvider.php b/app/Providers/ValidationServiceProvider.php index 2a675fa8d1..ea9c322765 100644 --- a/app/Providers/ValidationServiceProvider.php +++ b/app/Providers/ValidationServiceProvider.php @@ -58,6 +58,52 @@ class ValidationServiceProvider extends ServiceProvider }); + // Prevent circular references + // + // Example usage in Location model where parent_id references another Location: + // + // protected $rules = array( + // 'parent_id' => 'non_circular:locations,id,10' + // ); + // + Validator::extend('non_circular', function ($attribute, $value, $parameters, $validator) { + if (count($parameters) < 2) { + throw new \Exception('Required validator parameters: