From 7d77fe57c51a9602abc26a63806ed9249cb6a886 Mon Sep 17 00:00:00 2001 From: Andy Heathershaw Date: Tue, 18 Sep 2018 15:50:12 +0100 Subject: [PATCH] #4: If a validation error occurs while replying to a comment, the form is now re-rendered instead of JSON and a 422 being returned. --- .../Gallery/PhotoCommentController.php | 67 +++++++++++++------ .../Requests/StorePhotoCommentRequest.php | 32 --------- resources/assets/js/gallery.js | 9 ++- 3 files changed, 54 insertions(+), 54 deletions(-) delete mode 100644 app/Http/Requests/StorePhotoCommentRequest.php diff --git a/app/Http/Controllers/Gallery/PhotoCommentController.php b/app/Http/Controllers/Gallery/PhotoCommentController.php index be02f06..60758da 100644 --- a/app/Http/Controllers/Gallery/PhotoCommentController.php +++ b/app/Http/Controllers/Gallery/PhotoCommentController.php @@ -11,6 +11,7 @@ use App\PhotoComment; use Illuminate\Http\Request; use Illuminate\Support\Facades\App; use Illuminate\Support\Facades\Auth; +use Illuminate\Validation\ValidationException; class PhotoCommentController extends Controller { @@ -45,7 +46,7 @@ class PhotoCommentController extends Controller ]); } - public function store(StorePhotoCommentRequest $request, $albumUrlAlias, $photoFilename) + public function store(Request $request, $albumUrlAlias, $photoFilename) { $album = DbHelper::getAlbumByPath($albumUrlAlias); if (is_null($album)) @@ -64,11 +65,9 @@ class PhotoCommentController extends Controller return redirect($photo->url()); } - $comment = new PhotoComment(); - $comment->photo_id = $photo->id; - $comment->fill($request->only(['commentor_email', 'commentor_name', 'comment_text'])); - // Validate and link the parent comment, if provided + // We do this here so if the validation fails, we still have the parent comment available in the catch block + $parentComment = null; if ($request->has('parent_comment_id')) { $parentComment = $photo->comments()->where('id', intval($request->get('parent_comment_id')))->first(); @@ -78,27 +77,53 @@ class PhotoCommentController extends Controller $request->getSession()->flash('success', trans('gallery.photo_comment_posted_successfully')); return redirect($photo->url()); } - - $comment->parent_comment_id = $parentComment->id; } - $user = $this->getUser(); - if (!is_null($user) && !$user->isAnonymous()) + try { - $comment->created_user_id = $user->id; + $this->validate($request, [ + 'commentor_name' => 'required|max:255', + 'commentor_email' => 'sometimes|max:255|email', + 'comment_text' => 'required' + ]); + + $comment = new PhotoComment(); + $comment->photo_id = $photo->id; + $comment->fill($request->only(['commentor_email', 'commentor_name', 'comment_text'])); + + if (!is_null($parentComment)) + { + $comment->parent_comment_id = $parentComment->id; + } + + $user = $this->getUser(); + if (!is_null($user) && !$user->isAnonymous()) + { + $comment->created_user_id = $user->id; + } + + $comment->save(); + + $request->getSession()->flash('success', trans('gallery.photo_comment_posted_successfully')); + + if ($request->isXmlHttpRequest()) + { + return response()->json(['redirect_url' => $photo->url()]); + } else + { + return redirect($photo->url()); + } } - - $comment->save(); - - $request->getSession()->flash('success', trans('gallery.photo_comment_posted_successfully')); - - if ($request->isXmlHttpRequest()) + catch (ValidationException $e) { - return response()->json(['redirect_url' => $photo->url()]); - } - else - { - return redirect($photo->url()); + if (!is_null($parentComment)) + { + return redirect()->to($photo->replyToCommentFormUrl($parentComment->id))->withErrors($e->errors()); + } + else + { + return redirect()->back()->withErrors($e->errors()); + } } } } \ No newline at end of file diff --git a/app/Http/Requests/StorePhotoCommentRequest.php b/app/Http/Requests/StorePhotoCommentRequest.php deleted file mode 100644 index d131c21..0000000 --- a/app/Http/Requests/StorePhotoCommentRequest.php +++ /dev/null @@ -1,32 +0,0 @@ - 'required|max:255', - 'commentor_email' => 'sometimes|max:255|email', - 'comment_text' => 'required' - ]; - } -} \ No newline at end of file diff --git a/resources/assets/js/gallery.js b/resources/assets/js/gallery.js index 5355647..e027c09 100644 --- a/resources/assets/js/gallery.js +++ b/resources/assets/js/gallery.js @@ -40,7 +40,14 @@ function PhotoViewModel(urls) { var formData = form.serialize(); $.post(formUrl, formData, function(result) { - window.location = result.redirect_url; + if (result.redirect_url) + { + window.location = result.redirect_url; + } + else + { + $('#comment-reply-form-content').html(result); + } }); }, replyToComment: function(e) {