From 4c15271d36dd6a411508e2180546289a91fe2cb7 Mon Sep 17 00:00:00 2001 From: Hugo Sales Date: Tue, 8 Sep 2020 00:12:33 +0000 Subject: [PATCH] [UI] Display error when submitted form is invalid --- components/Posting/Controller/Post.php | 7 ++--- components/Posting/Posting.php | 3 ++- plugins/Favourite/Favourite.php | 15 ++++++++--- plugins/Repeat/Repeat.php | 16 +++++++---- src/Controller/AdminPanel.php | 3 ++- src/Util/Exception/InvalidFormException.php | 30 +++++++++++++++++++++ src/Util/Exception/NoSuchNoteException.php | 30 +++++++++++++++++++++ 7 files changed, 90 insertions(+), 14 deletions(-) create mode 100644 src/Util/Exception/InvalidFormException.php create mode 100644 src/Util/Exception/NoSuchNoteException.php diff --git a/components/Posting/Controller/Post.php b/components/Posting/Controller/Post.php index 7bcd061f62..32028ce051 100644 --- a/components/Posting/Controller/Post.php +++ b/components/Posting/Controller/Post.php @@ -28,7 +28,8 @@ use App\Core\Security; use App\Entity\FileToNote; use App\Entity\Note; use App\Util\Common; -use App\Util\Exception\ClientException; +use App\Util\Exceptiion\InvalidFormException; +use App\Util\Exception\NoSuchNoteException; use Component\Media\Media; use Symfony\Component\Form\Extension\Core\Type\FileType; use Symfony\Component\Form\Extension\Core\Type\HiddenType; @@ -42,7 +43,7 @@ class Post { $note = DB::find('note', ['id' => $reply_to]); if ($note == null) { - throw new ClientException(_m('No such note')); + throw new NoSuchNoteException(); } $actor_id = Common::ensureLoggedIn()->getId(); @@ -61,7 +62,7 @@ class Post if ($form->isValid()) { self::storeNote($actor_id, $data['content'], $data['attachments'], $is_local = true, $data['reply_to'], null); } else { - // TODO display errors + throw new InvalidFormException(); } } diff --git a/components/Posting/Posting.php b/components/Posting/Posting.php index 9b2cf4d6c8..dfd6b80a33 100644 --- a/components/Posting/Posting.php +++ b/components/Posting/Posting.php @@ -25,6 +25,7 @@ use App\Core\Form; use function App\Core\I18n\_m; use App\Core\Module; use App\Util\Common; +use App\Util\Exceptiion\InvalidFormException; use App\Util\Exception\RedirectException; use Component\Posting\Controller as C; use Symfony\Component\Form\Extension\Core\Type\ChoiceType; @@ -71,7 +72,7 @@ class Posting extends Module C\Post::storeNote($actor_id, $data['content'], $data['attachments'], $is_local = true); throw new RedirectException(); } else { - // TODO Display error + throw new InvalidFormException(); } } diff --git a/plugins/Favourite/Favourite.php b/plugins/Favourite/Favourite.php index b2e03e9b33..3db1dab8c3 100644 --- a/plugins/Favourite/Favourite.php +++ b/plugins/Favourite/Favourite.php @@ -26,6 +26,7 @@ use App\Core\Module; use App\Entity\Favourite as Fave; use App\Entity\Note; use App\Util\Common; +use App\Util\Exceptiion\InvalidFormException; use Symfony\Component\Form\Extension\Core\Type\HiddenType; use Symfony\Component\Form\Extension\Core\Type\SubmitType; use Symfony\Component\HttpFoundation\Request; @@ -43,8 +44,8 @@ class Favourite extends Module $opts = ['note_id' => $note->getId(), 'gsactor_id' => $user->getId()]; $is_set = DB::find('favourite', $opts) != null; $form = Form::create([ - ['is_set', HiddenType::class, ['data' => $is_set ? '1' : '0']], - ['note_id', HiddenType::class, ['data' => $note->getId()]], + ['is_set', HiddenType::class, ['data' => $is_set ? '1' : '0']], + ['note_id', HiddenType::class, ['data' => $note->getId()]], ['favourite', SubmitType::class, ['label' => ' ']], ]); @@ -52,8 +53,13 @@ class Favourite extends Module $form->handleRequest($request); if ($form->isSubmitted()) { $data = $form->getData(); + // Loose comparison + if ($data['note_id'] != $note->getId()) { + return Event::next; + } + $fave = DB::find('favourite', $opts); - if ($data['note_id'] == $note->getId() && $form->isValid()) { + if ($form->isValid()) { // Loose comparison if (!$data['is_set'] && ($fave == null)) { DB::persist(Fave::create($opts)); @@ -62,8 +68,9 @@ class Favourite extends Module DB::remove($fave); DB::flush(); } + return Event::stop; } else { - // TODO display errors + throw new InvalidFormException(); } } } diff --git a/plugins/Repeat/Repeat.php b/plugins/Repeat/Repeat.php index a3f055eacc..bba86d939a 100644 --- a/plugins/Repeat/Repeat.php +++ b/plugins/Repeat/Repeat.php @@ -25,6 +25,7 @@ use App\Core\Form; use App\Core\Module; use App\Entity\Note; use App\Util\Common; +use App\Util\Exceptiion\InvalidFormException; use Symfony\Component\Form\Extension\Core\Type\HiddenType; use Symfony\Component\Form\Extension\Core\Type\SubmitType; use Symfony\Component\HttpFoundation\Request; @@ -42,17 +43,21 @@ class Repeat extends Module $to_repeat = DB::find('note', ['id' => $note->getId()]); $is_set = false; $form = Form::create([ - ['is_set', HiddenType::class, ['data' => $is_set ? '1' : '0']], + ['is_set', HiddenType::class, ['data' => $is_set ? '1' : '0']], ['note_id', HiddenType::class, ['data' => $note->getId()]], - ['repeat', SubmitType::class, ['label' => ' ']], + ['repeat', SubmitType::class, ['label' => ' ']], ]); if ('POST' === $request->getMethod() && $request->request->has('repeat')) { $form->handleRequest($request); if ($form->isSubmitted()) { $data = $form->getData(); - if ($data['note_id'] == $to_repeat && $form->isValid()) { - // Loose comparison + // Loose comparison + if ($data['note_id'] != $to_repeat) { + return Event::next; + } + + if ($form->isValid()) { if (!$data['is_set']) { DB::persist(Note::create(['gsactor_id' => $user->getId(), 'repeat_of' => $note->getId(), 'content' => $note->getContent(), 'is_local' => true])); DB::flush(); @@ -60,8 +65,9 @@ class Repeat extends Module DB::remove($to_repeat); DB::flush(); } + return Event::stop; } else { - // TODO display errors + throw new InvalidFormException(); } } } diff --git a/src/Controller/AdminPanel.php b/src/Controller/AdminPanel.php index 2ba9bcc349..2c6776199f 100644 --- a/src/Controller/AdminPanel.php +++ b/src/Controller/AdminPanel.php @@ -37,6 +37,7 @@ use App\Core\DB\DefaultSettings; use App\Core\Form; use function App\Core\I18n\_m; use App\Util\Common; +use App\Util\Exceptiion\InvalidFormException; use App\Util\Formatting; use Symfony\Component\Form\Extension\Core\Type\ChoiceType; use Symfony\Component\Form\Extension\Core\Type\SubmitType; @@ -89,7 +90,7 @@ class AdminPanel extends Controller ]; } } else { - // TODO Display error + throw new InvalidFormException(); } } diff --git a/src/Util/Exception/InvalidFormException.php b/src/Util/Exception/InvalidFormException.php new file mode 100644 index 0000000000..654a5c1ef2 --- /dev/null +++ b/src/Util/Exception/InvalidFormException.php @@ -0,0 +1,30 @@ +. + +// }}} + +namespace App\Util\Exception; + +class InvalidFormException +{ + public function __construct() + { + parent::__construct(_m('Invalid form submitted')); + } +} diff --git a/src/Util/Exception/NoSuchNoteException.php b/src/Util/Exception/NoSuchNoteException.php new file mode 100644 index 0000000000..94ab3b3ae5 --- /dev/null +++ b/src/Util/Exception/NoSuchNoteException.php @@ -0,0 +1,30 @@ +. + +// }}} + +namespace App\Util\Exception; + +class NoSuchNoteException extends ClientException +{ + public function __construct() + { + parent::__construct(_m('No such note')); + } +}