diff --git a/skolehjem/app/Http/Controllers/WashingMachineController.php b/skolehjem/app/Http/Controllers/WashingMachineController.php index bb9965a..921e5bb 100644 --- a/skolehjem/app/Http/Controllers/WashingMachineController.php +++ b/skolehjem/app/Http/Controllers/WashingMachineController.php @@ -70,7 +70,7 @@ class WashingMachineController extends Controller // If there already is a washing machine with that name, then don't add it if (count($allMachines) > 0) - return redirect()->route("washing-machines.store")->with('WashingNameExists', '

Der findes allerede en vaskemaskine med det navn!

'); + return redirect()->route("washing-machines.store"); else { // Else - Add it $machine->save(); $machines = WashingMachine::query()->paginate($request->input("limit", 20)); @@ -127,7 +127,7 @@ class WashingMachineController extends Controller // If there already is a washing machine with that name, then don't change it if (count($allMachines) > 0) - return redirect()->route("washing-machines.store")->with('WashingNameExists', '

Der findes allerede en vaskemaskine med det navn!

'); + return redirect()->route("washing-machines.store"); else { // Else - Change the name $machine->update($data); $machine->save(); @@ -154,14 +154,17 @@ class WashingMachineController extends Controller return redirect()->route("washing-machines.index"); } - - - public function nameCheck(Request $request){ - $washing = WashingMachine::query()->where('name', 'LIKE',$request->nameCheck)->get(); + $washing = WashingMachine::query()->where('name', 'LIKE',$request->nameCheck)->where('location_id', '=', $request->location)->get(); if(count($washing) > 0 && $request->nameCheck !== NULL){ return 1; } } + public function nameCheckUpdate(Request $request){ + $washing = WashingMachine::query()->where('name', 'LIKE',$request->nameCheck)->where('location_id', '=', $request->location)->where('id', '!=', $request->id)->get(); + if(count($washing) > 0 && $request->nameCheck !== NULL){ + return 1; + } + } } diff --git a/skolehjem/resources/views/admin/washing-machines/create.blade.php b/skolehjem/resources/views/admin/washing-machines/create.blade.php index a8a803b..efd35d7 100644 --- a/skolehjem/resources/views/admin/washing-machines/create.blade.php +++ b/skolehjem/resources/views/admin/washing-machines/create.blade.php @@ -14,7 +14,7 @@
@csrf - + - +
@endsection @section("scripts") @@ -38,5 +39,82 @@ } ); }); + + //Dont delete me, if the user reloads the pages with text in the text-box this code runs. + $(document).ready(function () { + $value = $('#name').val(); + $.ajax({ + type: 'get', + url: '{{route('washing-machines.nameCheckUpdate')}}', + data: {'nameCheck':$value, 'location': $('#location_id').val(), 'id': {{$machine->id}}}, + success:function (data) { + console.log(data); + if(data){ + $("#error").show(100); + $("#error").css('color', 'red'); + $("#disable").prop('disabled', true); + $('#disable').css('cursor','not-allowed'); + }else{ + $("#error").hide(); + $("#disable").prop('disabled', false); + $('#disable').css('cursor','pointer'); + } + + }, + error:function (data) { + console.log(data); + } + }); + }); + + $('#name').on('keyup', function () { + $value = $(this).val(); + $.ajax({ + type: 'get', + url: '{{route('washing-machines.nameCheckUpdate')}}', + data: {'nameCheck':$value, 'location': $('#location_id').val(), 'id': {{$machine->id}}}, + success:function (data) { + if(data){ + $("#error").show(100); + $("#error").css('color', 'red'); + $("#disable").prop('disabled', true); + $('#disable').css('cursor','not-allowed'); + }else{ + $("#error").hide(); + $("#disable").prop('disabled', false); + $('#disable').css('cursor','pointer'); + } + + }, + error:function (data) { + console.log(data); + } + }); + }) + + $('#location_id').on('change', function () { + $value = $('#name').val(); + $.ajax({ + type: 'get', + url: '{{route('washing-machines.nameCheckUpdate')}}', + data: {'nameCheck':$value, 'location': $('#location_id').val(), 'id': {{$machine->id}}}, + success:function (data) { + if(data){ + $("#error").show(100); + $("#error").css('color', 'red'); + $("#disable").prop('disabled', true); + $('#disable').css('cursor','not-allowed'); + }else{ + $("#error").hide(); + $("#disable").prop('disabled', false); + $('#disable').css('cursor','pointer'); + } + + }, + error:function (data) { + console.log(data); + } + }); + }) @endsection diff --git a/skolehjem/resources/views/admin/washing-machines/index.blade.php b/skolehjem/resources/views/admin/washing-machines/index.blade.php index f8332da..8ddd5ec 100644 --- a/skolehjem/resources/views/admin/washing-machines/index.blade.php +++ b/skolehjem/resources/views/admin/washing-machines/index.blade.php @@ -13,7 +13,6 @@
CreateOpret Vaskemaskine
- {!! session()->get('WashingNameExists') !!} diff --git a/skolehjem/routes/web.php b/skolehjem/routes/web.php index 6bd705f..594557d 100644 --- a/skolehjem/routes/web.php +++ b/skolehjem/routes/web.php @@ -73,6 +73,7 @@ Route::get("/guidesCheckUpdate", "GuideController@nameCheckUpdate")->name("guide Route::get("/newsCheckUpdate", "NewsController@nameCheckUpdate")->name("news.nameCheckUpdate"); Route::get("/menuplanCheckUpdate", "MenuPlanController@nameCheckUpdate")->name("menuplans.nameCheckUpdate"); Route::get("/locationsCheckUpdate", "LocationController@nameCheckUpdate")->name("locations.nameCheckUpdate"); +Route::get("/washingCheckUpdate", "WashingMachineController@nameCheckUpdate")->name("washing-machines.nameCheckUpdate");
Navn