From e6e0e3c95d786406a6a0237a060967481c253f08 Mon Sep 17 00:00:00 2001 From: Anders Date: Tue, 18 Aug 2020 08:16:25 +0200 Subject: [PATCH] v0.10.2 - Full functioning AJAX check submit code on User controller create & edit - Plus all other pages where you create or update something of an unique value --- .../app/Http/Controllers/UserController.php | 20 +++++- .../views/admin/users/create.blade.php | 10 +-- .../views/admin/users/edit.blade.php | 61 +++++++++++++++++-- skolehjem/routes/web.php | 1 + 4 files changed, 79 insertions(+), 13 deletions(-) diff --git a/skolehjem/app/Http/Controllers/UserController.php b/skolehjem/app/Http/Controllers/UserController.php index eb14e48..0d65e75 100644 --- a/skolehjem/app/Http/Controllers/UserController.php +++ b/skolehjem/app/Http/Controllers/UserController.php @@ -580,13 +580,27 @@ class UserController extends Controller $usersemail = User::query()->where('email', 'LIKE', $request->mailCheck)->get(); $usersphone = User::query()->where('phone', '=', $request->phoneCheck)->get(); if (count($usersemail) > 0 && count($usersphone) > 0) { - return 1; + return 'both'; } elseif(count($usersemail) > 0 && $request->mailCheck !== NULL){ - return 2; + return 'email'; } elseif(count($usersphone) > 0 && $request->phoneCheck !== NULL){ - return 3; + return 'phone'; + } + } + + public function nameCheckUpdate(Request $request){ + $usersemail = User::query()->where('email', 'LIKE', $request->mailCheck)->where('id', '!=', $request->id)->get(); + $usersphone = User::query()->where('phone', '=', $request->phoneCheck)->where('id', '!=', $request->id)->get(); + if (count($usersemail) > 0 && count($usersphone) > 0) { + return 'both'; + } + elseif(count($usersemail) > 0 && $request->mailCheck !== NULL){ + return 'email'; + } + elseif(count($usersphone) > 0 && $request->phoneCheck !== NULL){ + return 'phone'; } } } diff --git a/skolehjem/resources/views/admin/users/create.blade.php b/skolehjem/resources/views/admin/users/create.blade.php index 89e0cf7..aebea0f 100644 --- a/skolehjem/resources/views/admin/users/create.blade.php +++ b/skolehjem/resources/views/admin/users/create.blade.php @@ -11,7 +11,7 @@ @section("content")

Opret Bruger:

-
+ @csrf @@ -71,7 +71,7 @@ ); }); - function myFunction() { + function checkInputs() { var save = true; $mail = $('#email').val(); $phone = $('#phone').val(); @@ -81,18 +81,18 @@ url: '{{route('users.nameCheck')}}', data: {'mailCheck':$mail, 'phoneCheck':$phone}, success:function (data) { - if (data == 1) { + if (data == 'both') { $("#erroremail").show(100); $("#erroremail").css('color', 'red'); $("#errorphone").show(100); $("#errorphone").css('color', 'red'); save = false; - } else if(data == 2){ + } else if(data == 'email'){ $("#errorphone").hide(); $("#erroremail").show(100); $("#erroremail").css('color', 'red'); save = false; - } else if (data == 3) { + } else if (data == 'phone') { $("#erroremail").hide(); $("#errorphone").show(100); $("#errorphone").css('color', 'red'); diff --git a/skolehjem/resources/views/admin/users/edit.blade.php b/skolehjem/resources/views/admin/users/edit.blade.php index 01f572c..a3e74f2 100644 --- a/skolehjem/resources/views/admin/users/edit.blade.php +++ b/skolehjem/resources/views/admin/users/edit.blade.php @@ -11,10 +11,7 @@ @section("content")

Rediger Bruger:

- {!! session()->get('error#notsamepass') !!} - {!! session()->get('EmailExists') !!} - {!! session()->get('PhoneExists') !!} - $user]) }}"> + $user]) }}" onsubmit="return checkInputs()"> @csrf @method("put") @@ -22,8 +19,10 @@ + +
@@ -34,7 +33,8 @@
- + +