[UI] Display error when submitted form is invalid

This commit is contained in:
Hugo Sales 2020-09-08 00:12:33 +00:00 committed by Hugo Sales
parent 5fc7647c40
commit 4c15271d36
Signed by: someonewithpc
GPG Key ID: 7D0C7EAFC9D835A0
7 changed files with 90 additions and 14 deletions

View File

@ -28,7 +28,8 @@ use App\Core\Security;
use App\Entity\FileToNote; use App\Entity\FileToNote;
use App\Entity\Note; use App\Entity\Note;
use App\Util\Common; use App\Util\Common;
use App\Util\Exception\ClientException; use App\Util\Exceptiion\InvalidFormException;
use App\Util\Exception\NoSuchNoteException;
use Component\Media\Media; use Component\Media\Media;
use Symfony\Component\Form\Extension\Core\Type\FileType; use Symfony\Component\Form\Extension\Core\Type\FileType;
use Symfony\Component\Form\Extension\Core\Type\HiddenType; use Symfony\Component\Form\Extension\Core\Type\HiddenType;
@ -42,7 +43,7 @@ class Post
{ {
$note = DB::find('note', ['id' => $reply_to]); $note = DB::find('note', ['id' => $reply_to]);
if ($note == null) { if ($note == null) {
throw new ClientException(_m('No such note')); throw new NoSuchNoteException();
} }
$actor_id = Common::ensureLoggedIn()->getId(); $actor_id = Common::ensureLoggedIn()->getId();
@ -61,7 +62,7 @@ class Post
if ($form->isValid()) { if ($form->isValid()) {
self::storeNote($actor_id, $data['content'], $data['attachments'], $is_local = true, $data['reply_to'], null); self::storeNote($actor_id, $data['content'], $data['attachments'], $is_local = true, $data['reply_to'], null);
} else { } else {
// TODO display errors throw new InvalidFormException();
} }
} }

View File

@ -25,6 +25,7 @@ use App\Core\Form;
use function App\Core\I18n\_m; use function App\Core\I18n\_m;
use App\Core\Module; use App\Core\Module;
use App\Util\Common; use App\Util\Common;
use App\Util\Exceptiion\InvalidFormException;
use App\Util\Exception\RedirectException; use App\Util\Exception\RedirectException;
use Component\Posting\Controller as C; use Component\Posting\Controller as C;
use Symfony\Component\Form\Extension\Core\Type\ChoiceType; 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); C\Post::storeNote($actor_id, $data['content'], $data['attachments'], $is_local = true);
throw new RedirectException(); throw new RedirectException();
} else { } else {
// TODO Display error throw new InvalidFormException();
} }
} }

View File

@ -26,6 +26,7 @@ use App\Core\Module;
use App\Entity\Favourite as Fave; use App\Entity\Favourite as Fave;
use App\Entity\Note; use App\Entity\Note;
use App\Util\Common; use App\Util\Common;
use App\Util\Exceptiion\InvalidFormException;
use Symfony\Component\Form\Extension\Core\Type\HiddenType; use Symfony\Component\Form\Extension\Core\Type\HiddenType;
use Symfony\Component\Form\Extension\Core\Type\SubmitType; use Symfony\Component\Form\Extension\Core\Type\SubmitType;
use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Request;
@ -43,8 +44,8 @@ class Favourite extends Module
$opts = ['note_id' => $note->getId(), 'gsactor_id' => $user->getId()]; $opts = ['note_id' => $note->getId(), 'gsactor_id' => $user->getId()];
$is_set = DB::find('favourite', $opts) != null; $is_set = DB::find('favourite', $opts) != null;
$form = Form::create([ $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()]], ['note_id', HiddenType::class, ['data' => $note->getId()]],
['favourite', SubmitType::class, ['label' => ' ']], ['favourite', SubmitType::class, ['label' => ' ']],
]); ]);
@ -52,8 +53,13 @@ class Favourite extends Module
$form->handleRequest($request); $form->handleRequest($request);
if ($form->isSubmitted()) { if ($form->isSubmitted()) {
$data = $form->getData(); $data = $form->getData();
// Loose comparison
if ($data['note_id'] != $note->getId()) {
return Event::next;
}
$fave = DB::find('favourite', $opts); $fave = DB::find('favourite', $opts);
if ($data['note_id'] == $note->getId() && $form->isValid()) { if ($form->isValid()) {
// Loose comparison // Loose comparison
if (!$data['is_set'] && ($fave == null)) { if (!$data['is_set'] && ($fave == null)) {
DB::persist(Fave::create($opts)); DB::persist(Fave::create($opts));
@ -62,8 +68,9 @@ class Favourite extends Module
DB::remove($fave); DB::remove($fave);
DB::flush(); DB::flush();
} }
return Event::stop;
} else { } else {
// TODO display errors throw new InvalidFormException();
} }
} }
} }

View File

@ -25,6 +25,7 @@ use App\Core\Form;
use App\Core\Module; use App\Core\Module;
use App\Entity\Note; use App\Entity\Note;
use App\Util\Common; use App\Util\Common;
use App\Util\Exceptiion\InvalidFormException;
use Symfony\Component\Form\Extension\Core\Type\HiddenType; use Symfony\Component\Form\Extension\Core\Type\HiddenType;
use Symfony\Component\Form\Extension\Core\Type\SubmitType; use Symfony\Component\Form\Extension\Core\Type\SubmitType;
use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Request;
@ -42,17 +43,21 @@ class Repeat extends Module
$to_repeat = DB::find('note', ['id' => $note->getId()]); $to_repeat = DB::find('note', ['id' => $note->getId()]);
$is_set = false; $is_set = false;
$form = Form::create([ $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()]], ['note_id', HiddenType::class, ['data' => $note->getId()]],
['repeat', SubmitType::class, ['label' => ' ']], ['repeat', SubmitType::class, ['label' => ' ']],
]); ]);
if ('POST' === $request->getMethod() && $request->request->has('repeat')) { if ('POST' === $request->getMethod() && $request->request->has('repeat')) {
$form->handleRequest($request); $form->handleRequest($request);
if ($form->isSubmitted()) { if ($form->isSubmitted()) {
$data = $form->getData(); $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']) { if (!$data['is_set']) {
DB::persist(Note::create(['gsactor_id' => $user->getId(), 'repeat_of' => $note->getId(), 'content' => $note->getContent(), 'is_local' => true])); DB::persist(Note::create(['gsactor_id' => $user->getId(), 'repeat_of' => $note->getId(), 'content' => $note->getContent(), 'is_local' => true]));
DB::flush(); DB::flush();
@ -60,8 +65,9 @@ class Repeat extends Module
DB::remove($to_repeat); DB::remove($to_repeat);
DB::flush(); DB::flush();
} }
return Event::stop;
} else { } else {
// TODO display errors throw new InvalidFormException();
} }
} }
} }

View File

@ -37,6 +37,7 @@ use App\Core\DB\DefaultSettings;
use App\Core\Form; use App\Core\Form;
use function App\Core\I18n\_m; use function App\Core\I18n\_m;
use App\Util\Common; use App\Util\Common;
use App\Util\Exceptiion\InvalidFormException;
use App\Util\Formatting; use App\Util\Formatting;
use Symfony\Component\Form\Extension\Core\Type\ChoiceType; use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
use Symfony\Component\Form\Extension\Core\Type\SubmitType; use Symfony\Component\Form\Extension\Core\Type\SubmitType;
@ -89,7 +90,7 @@ class AdminPanel extends Controller
]; ];
} }
} else { } else {
// TODO Display error throw new InvalidFormException();
} }
} }

View File

@ -0,0 +1,30 @@
<?php
// {{{ License
// This file is part of GNU social - https://www.gnu.org/software/social
//
// GNU social is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// GNU social is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with GNU social. If not, see <http://www.gnu.org/licenses/>.
// }}}
namespace App\Util\Exception;
class InvalidFormException
{
public function __construct()
{
parent::__construct(_m('Invalid form submitted'));
}
}

View File

@ -0,0 +1,30 @@
<?php
// {{{ License
// This file is part of GNU social - https://www.gnu.org/software/social
//
// GNU social is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// GNU social is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with GNU social. If not, see <http://www.gnu.org/licenses/>.
// }}}
namespace App\Util\Exception;
class NoSuchNoteException extends ClientException
{
public function __construct()
{
parent::__construct(_m('No such note'));
}
}