From 2c3e148b0029a094431622feac79fafcd0d43fc8 Mon Sep 17 00:00:00 2001 From: adev Date: Sun, 7 May 2017 17:21:30 +0200 Subject: Displays an error with an annotation with a too long quote Fix #2762 --- .../Controller/WallabagAnnotationController.php | 49 +++++++++++++--------- .../AnnotationBundle/Entity/Annotation.php | 7 +++- .../AnnotationBundle/Form/EditAnnotationType.php | 18 ++++++++ .../AnnotationBundle/Form/NewAnnotationType.php | 35 ++++++++++++++++ src/Wallabag/AnnotationBundle/Form/RangeType.php | 19 +++++++++ .../Resources/translations/validators.da.yml | 1 + .../Resources/translations/validators.de.yml | 1 + .../Resources/translations/validators.en.yml | 1 + .../Resources/translations/validators.es.yml | 1 + .../Resources/translations/validators.fa.yml | 1 + .../Resources/translations/validators.fr.yml | 1 + .../Resources/translations/validators.it.yml | 1 + .../Resources/translations/validators.oc.yml | 1 + .../Resources/translations/validators.pl.yml | 1 + .../Resources/translations/validators.pt.yml | 1 + .../Resources/translations/validators.ro.yml | 1 + .../Resources/translations/validators.tr.yml | 1 + 17 files changed, 119 insertions(+), 21 deletions(-) create mode 100644 src/Wallabag/AnnotationBundle/Form/EditAnnotationType.php create mode 100644 src/Wallabag/AnnotationBundle/Form/NewAnnotationType.php create mode 100644 src/Wallabag/AnnotationBundle/Form/RangeType.php (limited to 'src/Wallabag') diff --git a/src/Wallabag/AnnotationBundle/Controller/WallabagAnnotationController.php b/src/Wallabag/AnnotationBundle/Controller/WallabagAnnotationController.php index c13a034f..2b4b0e8d 100644 --- a/src/Wallabag/AnnotationBundle/Controller/WallabagAnnotationController.php +++ b/src/Wallabag/AnnotationBundle/Controller/WallabagAnnotationController.php @@ -7,6 +7,8 @@ use Symfony\Component\HttpFoundation\JsonResponse; use Symfony\Component\HttpFoundation\Request; use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter; use Wallabag\AnnotationBundle\Entity\Annotation; +use Wallabag\AnnotationBundle\Form\EditAnnotationType; +use Wallabag\AnnotationBundle\Form\NewAnnotationType; use Wallabag\CoreBundle\Entity\Entry; class WallabagAnnotationController extends FOSRestController @@ -49,25 +51,25 @@ class WallabagAnnotationController extends FOSRestController $data = json_decode($request->getContent(), true); $em = $this->getDoctrine()->getManager(); - $annotation = new Annotation($this->getUser()); + $annotation->setEntry($entry); - $annotation->setText($data['text']); - if (array_key_exists('quote', $data)) { - $annotation->setQuote($data['quote']); - } - if (array_key_exists('ranges', $data)) { - $annotation->setRanges($data['ranges']); - } + $form = $this->get('form.factory')->createNamed('', NewAnnotationType::class, $annotation, [ + 'csrf_protection' => false, + 'allow_extra_fields' => true, + ]); + $form->submit($data); - $annotation->setEntry($entry); + if ($form->isValid()) { + $em->persist($annotation); + $em->flush(); - $em->persist($annotation); - $em->flush(); + $json = $this->get('serializer')->serialize($annotation, 'json'); - $json = $this->get('serializer')->serialize($annotation, 'json'); + return JsonResponse::fromJsonString($json); + } - return (new JsonResponse())->setJson($json); + return $form; } /** @@ -86,16 +88,23 @@ class WallabagAnnotationController extends FOSRestController { $data = json_decode($request->getContent(), true); - if (!is_null($data['text'])) { - $annotation->setText($data['text']); - } + $form = $this->get('form.factory')->createNamed('', EditAnnotationType::class, $annotation, [ + 'csrf_protection' => false, + 'allow_extra_fields' => true, + ]); + $form->submit($data); - $em = $this->getDoctrine()->getManager(); - $em->flush(); + if ($form->isValid()) { + $em = $this->getDoctrine()->getManager(); + $em->persist($annotation); + $em->flush(); - $json = $this->get('serializer')->serialize($annotation, 'json'); + $json = $this->get('serializer')->serialize($annotation, 'json'); - return (new JsonResponse())->setJson($json); + return JsonResponse::fromJsonString($json); + } + + return $form; } /** diff --git a/src/Wallabag/AnnotationBundle/Entity/Annotation.php b/src/Wallabag/AnnotationBundle/Entity/Annotation.php index 0838f5aa..c8e41649 100644 --- a/src/Wallabag/AnnotationBundle/Entity/Annotation.php +++ b/src/Wallabag/AnnotationBundle/Entity/Annotation.php @@ -8,6 +8,7 @@ use JMS\Serializer\Annotation\Exclude; use JMS\Serializer\Annotation\VirtualProperty; use JMS\Serializer\Annotation\SerializedName; use JMS\Serializer\Annotation\Groups; +use Symfony\Component\Validator\Constraints as Assert; use Wallabag\UserBundle\Entity\User; use Wallabag\CoreBundle\Entity\Entry; @@ -56,7 +57,11 @@ class Annotation /** * @var string * - * @ORM\Column(name="quote", type="string") + * @Assert\Length( + * max = 10000, + * maxMessage = "validator.quote_length_too_high" + * ) + * @ORM\Column(name="quote", type="text") * * @Groups({"entries_for_user", "export_all"}) */ diff --git a/src/Wallabag/AnnotationBundle/Form/EditAnnotationType.php b/src/Wallabag/AnnotationBundle/Form/EditAnnotationType.php new file mode 100644 index 00000000..3b587478 --- /dev/null +++ b/src/Wallabag/AnnotationBundle/Form/EditAnnotationType.php @@ -0,0 +1,18 @@ +add('text', null, [ + 'empty_data' => '', + ]) + ; + } +} diff --git a/src/Wallabag/AnnotationBundle/Form/NewAnnotationType.php b/src/Wallabag/AnnotationBundle/Form/NewAnnotationType.php new file mode 100644 index 00000000..c73c3ded --- /dev/null +++ b/src/Wallabag/AnnotationBundle/Form/NewAnnotationType.php @@ -0,0 +1,35 @@ +add('text', null, [ + 'empty_data' => '', + ]) + ->add('quote', null, [ + 'empty_data' => null, + ]) + ->add('ranges', CollectionType::class, [ + 'entry_type' => RangeType::class, + 'allow_add' => true, + ]) + ; + } + + public function configureOptions(OptionsResolver $resolver) + { + $resolver->setDefaults([ + 'data_class' => Annotation::class, + ]); + } +} diff --git a/src/Wallabag/AnnotationBundle/Form/RangeType.php b/src/Wallabag/AnnotationBundle/Form/RangeType.php new file mode 100644 index 00000000..0647375e --- /dev/null +++ b/src/Wallabag/AnnotationBundle/Form/RangeType.php @@ -0,0 +1,19 @@ +add('start') + ->add('startOffset') + ->add('end') + ->add('endOffset') + ; + } +} diff --git a/src/Wallabag/CoreBundle/Resources/translations/validators.da.yml b/src/Wallabag/CoreBundle/Resources/translations/validators.da.yml index 32a8b4a8..c6a84209 100644 --- a/src/Wallabag/CoreBundle/Resources/translations/validators.da.yml +++ b/src/Wallabag/CoreBundle/Resources/translations/validators.da.yml @@ -4,3 +4,4 @@ validator: # password_wrong_value: 'Wrong value for your current password' # item_per_page_too_high: 'This will certainly kill the app' # rss_limit_too_high: 'This will certainly kill the app' + # quote_length_too_high: 'The quote is too long. It should have {{ limit }} characters or less.' diff --git a/src/Wallabag/CoreBundle/Resources/translations/validators.de.yml b/src/Wallabag/CoreBundle/Resources/translations/validators.de.yml index 37b9888f..c74c00ca 100644 --- a/src/Wallabag/CoreBundle/Resources/translations/validators.de.yml +++ b/src/Wallabag/CoreBundle/Resources/translations/validators.de.yml @@ -4,3 +4,4 @@ validator: password_wrong_value: 'Falscher Wert für dein aktuelles Kennwort' item_per_page_too_high: 'Dies wird die Anwendung möglicherweise beenden' rss_limit_too_high: 'Dies wird die Anwendung möglicherweise beenden' + # quote_length_too_high: 'The quote is too long. It should have {{ limit }} characters or less.' diff --git a/src/Wallabag/CoreBundle/Resources/translations/validators.en.yml b/src/Wallabag/CoreBundle/Resources/translations/validators.en.yml index 29217497..8cc117fe 100644 --- a/src/Wallabag/CoreBundle/Resources/translations/validators.en.yml +++ b/src/Wallabag/CoreBundle/Resources/translations/validators.en.yml @@ -4,3 +4,4 @@ validator: password_wrong_value: 'Wrong value for your current password' item_per_page_too_high: 'This will certainly kill the app' rss_limit_too_high: 'This will certainly kill the app' + quote_length_too_high: 'The quote is too long. It should have {{ limit }} characters or less.' diff --git a/src/Wallabag/CoreBundle/Resources/translations/validators.es.yml b/src/Wallabag/CoreBundle/Resources/translations/validators.es.yml index 57ddaa5a..97a8edfa 100644 --- a/src/Wallabag/CoreBundle/Resources/translations/validators.es.yml +++ b/src/Wallabag/CoreBundle/Resources/translations/validators.es.yml @@ -4,3 +4,4 @@ validator: password_wrong_value: 'Entrada equivocada para su contraseña actual' item_per_page_too_high: 'Esto matará la aplicación' rss_limit_too_high: 'Esto matará la aplicación' + # quote_length_too_high: 'The quote is too long. It should have {{ limit }} characters or less.' diff --git a/src/Wallabag/CoreBundle/Resources/translations/validators.fa.yml b/src/Wallabag/CoreBundle/Resources/translations/validators.fa.yml index e0536d18..ef677525 100644 --- a/src/Wallabag/CoreBundle/Resources/translations/validators.fa.yml +++ b/src/Wallabag/CoreBundle/Resources/translations/validators.fa.yml @@ -4,3 +4,4 @@ validator: password_wrong_value: 'رمز فعلی را اشتباه وارد کرده‌اید' item_per_page_too_high: 'با این تعداد برنامه به فنا می‌رود' rss_limit_too_high: 'با این تعداد برنامه به فنا می‌رود' + # quote_length_too_high: 'The quote is too long. It should have {{ limit }} characters or less.' diff --git a/src/Wallabag/CoreBundle/Resources/translations/validators.fr.yml b/src/Wallabag/CoreBundle/Resources/translations/validators.fr.yml index 64574709..f31b4ed2 100644 --- a/src/Wallabag/CoreBundle/Resources/translations/validators.fr.yml +++ b/src/Wallabag/CoreBundle/Resources/translations/validators.fr.yml @@ -4,3 +4,4 @@ validator: password_wrong_value: "Votre mot de passe actuel est faux" item_per_page_too_high: "Ça ne va pas plaire à l’application" rss_limit_too_high: "Ça ne va pas plaire à l’application" + quote_length_too_high: "La citation est trop longue. Elle doit avoir au maximum {{ limit }} caractères." diff --git a/src/Wallabag/CoreBundle/Resources/translations/validators.it.yml b/src/Wallabag/CoreBundle/Resources/translations/validators.it.yml index d9beb54f..d949cc3b 100644 --- a/src/Wallabag/CoreBundle/Resources/translations/validators.it.yml +++ b/src/Wallabag/CoreBundle/Resources/translations/validators.it.yml @@ -4,3 +4,4 @@ validator: password_wrong_value: 'Valore inserito per la password corrente errato' item_per_page_too_high: 'Questo valore è troppo alto' rss_limit_too_high: 'Questo valore è troppo alto' + # quote_length_too_high: 'The quote is too long. It should have {{ limit }} characters or less.' diff --git a/src/Wallabag/CoreBundle/Resources/translations/validators.oc.yml b/src/Wallabag/CoreBundle/Resources/translations/validators.oc.yml index f92c2708..fb4aa592 100644 --- a/src/Wallabag/CoreBundle/Resources/translations/validators.oc.yml +++ b/src/Wallabag/CoreBundle/Resources/translations/validators.oc.yml @@ -4,3 +4,4 @@ validator: password_wrong_value: 'Vòstre senhal actual es pas bon' item_per_page_too_high: "Aquò li agradarà pas a l'aplicacion" rss_limit_too_high: "Aquò li agradarà pas a l'aplicacion" + # quote_length_too_high: 'The quote is too long. It should have {{ limit }} characters or less.' diff --git a/src/Wallabag/CoreBundle/Resources/translations/validators.pl.yml b/src/Wallabag/CoreBundle/Resources/translations/validators.pl.yml index ffcd5e7f..58d19cc9 100644 --- a/src/Wallabag/CoreBundle/Resources/translations/validators.pl.yml +++ b/src/Wallabag/CoreBundle/Resources/translations/validators.pl.yml @@ -4,3 +4,4 @@ validator: password_wrong_value: 'Twoje obecne hasło jest błędne' item_per_page_too_high: 'To może spowodować problemy z aplikacją' rss_limit_too_high: 'To może spowodować problemy z aplikacją' + # quote_length_too_high: 'The quote is too long. It should have {{ limit }} characters or less.' diff --git a/src/Wallabag/CoreBundle/Resources/translations/validators.pt.yml b/src/Wallabag/CoreBundle/Resources/translations/validators.pt.yml index 4eddff10..a8c1f9de 100644 --- a/src/Wallabag/CoreBundle/Resources/translations/validators.pt.yml +++ b/src/Wallabag/CoreBundle/Resources/translations/validators.pt.yml @@ -4,3 +4,4 @@ validator: password_wrong_value: 'A senha atual informada está errada' item_per_page_too_high: 'Certamente isso pode matar a aplicação' rss_limit_too_high: 'Certamente isso pode matar a aplicação' + # quote_length_too_high: 'The quote is too long. It should have {{ limit }} characters or less.' diff --git a/src/Wallabag/CoreBundle/Resources/translations/validators.ro.yml b/src/Wallabag/CoreBundle/Resources/translations/validators.ro.yml index 59a8cdd8..6840cf11 100644 --- a/src/Wallabag/CoreBundle/Resources/translations/validators.ro.yml +++ b/src/Wallabag/CoreBundle/Resources/translations/validators.ro.yml @@ -4,3 +4,4 @@ validator: # password_wrong_value: 'Wrong value for your current password' # item_per_page_too_high: 'This will certainly kill the app' # rss_limit_too_high: 'This will certainly kill the app' + # quote_length_too_high: 'The quote is too long. It should have {{ limit }} characters or less.' diff --git a/src/Wallabag/CoreBundle/Resources/translations/validators.tr.yml b/src/Wallabag/CoreBundle/Resources/translations/validators.tr.yml index 01388771..e1e7317f 100644 --- a/src/Wallabag/CoreBundle/Resources/translations/validators.tr.yml +++ b/src/Wallabag/CoreBundle/Resources/translations/validators.tr.yml @@ -4,3 +4,4 @@ validator: # password_wrong_value: 'Wrong value for your current password' # item_per_page_too_high: 'This will certainly kill the app' # rss_limit_too_high: 'This will certainly kill the app' + # quote_length_too_high: 'The quote is too long. It should have {{ limit }} characters or less.' -- cgit v1.2.3