diff --git a/js/forum/src/components/DiscussionComposer.js b/js/forum/src/components/DiscussionComposer.js index 7049e21ff..dbf513a9e 100644 --- a/js/forum/src/components/DiscussionComposer.js +++ b/js/forum/src/components/DiscussionComposer.js @@ -108,7 +108,11 @@ export default class DiscussionComposer extends ComposerBody { app.cache.discussionList.addDiscussion(discussion); m.route(app.route.discussion(discussion)); }, - () => this.loading = false + response => { + this.loading = false; + m.redraw(); + app.alertErrors(response.errors); + } ); } } diff --git a/js/forum/src/components/ReplyComposer.js b/js/forum/src/components/ReplyComposer.js index dd9db1e17..32d96ad60 100644 --- a/js/forum/src/components/ReplyComposer.js +++ b/js/forum/src/components/ReplyComposer.js @@ -92,10 +92,10 @@ export default class ReplyComposer extends ComposerBody { app.composer.hide(); }, - errors => { + response => { this.loading = false; m.redraw(); - app.alertErrors(errors); + app.alertErrors(response.errors); } ); } diff --git a/js/lib/App.js b/js/lib/App.js index 28cd1c6f2..0ae388437 100644 --- a/js/lib/App.js +++ b/js/lib/App.js @@ -215,8 +215,8 @@ export default class App { alertErrors(errors) { errors.forEach(error => { this.alerts.show(new Alert({ - type: 'warning', - message: error.detail + type: 'error', + children: error.detail })); }); } diff --git a/src/Api/Actions/JsonApiAction.php b/src/Api/Actions/JsonApiAction.php index 5697d8f22..b81b057ce 100644 --- a/src/Api/Actions/JsonApiAction.php +++ b/src/Api/Actions/JsonApiAction.php @@ -18,7 +18,7 @@ abstract class JsonApiAction implements Action */ public function handle(Request $request) { - // TODO: Move this error handling code to middleware? + // TODO: This is gross. Move this error handling code to middleware? try { return $this->respond($request); } catch (ValidationException $e) { @@ -30,6 +30,12 @@ abstract class JsonApiAction implements Action ]; } return new JsonResponse(['errors' => $errors], 422); + } catch (\Flarum\Core\Exceptions\ValidationException $e) { + $errors = []; + foreach ($e->getMessages() as $path => $detail) { + $errors[] = compact('path', 'detail'); + } + return new JsonResponse(['errors' => $errors], 422); } catch (PermissionDeniedException $e) { return new JsonResponse(null, 401); } catch (ModelNotFoundException $e) { diff --git a/src/Core/Exceptions/ValidationException.php b/src/Core/Exceptions/ValidationException.php new file mode 100644 index 000000000..f517161cd --- /dev/null +++ b/src/Core/Exceptions/ValidationException.php @@ -0,0 +1,18 @@ +messages = $messages; + } + + public function getMessages() + { + return $this->messages; + } +}