From fd8b6490d0fd05ae46bb40e8bb1309e57c565c2d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Julien=20Fastr=C3=A9?= Date: Tue, 17 Apr 2018 11:39:16 +0200 Subject: [PATCH] add validation to single task controller::new --- Controller/SingleTaskController.php | 23 +++++++++++++++-------- Entity/AbstractTask.php | 15 ++++++++++++--- Entity/SingleTask.php | 7 +++++++ Form/SingleTaskType.php | 13 +++++++++---- Resources/translations/validators.fr.yml | 1 + 5 files changed, 44 insertions(+), 15 deletions(-) create mode 100644 Resources/translations/validators.fr.yml diff --git a/Controller/SingleTaskController.php b/Controller/SingleTaskController.php index b2158d0bd..1594b3e7b 100644 --- a/Controller/SingleTaskController.php +++ b/Controller/SingleTaskController.php @@ -13,6 +13,7 @@ use Chill\TaskBundle\Form\SingleTaskType; use Symfony\Component\Form\Extension\Core\Type\SubmitType; use Symfony\Component\Form\FormFactoryInterface; use Chill\TaskBundle\Security\Authorization\TaskVoter; +use Symfony\Component\Security\Core\Role\Role; class SingleTaskController extends Controller { @@ -58,6 +59,7 @@ class SingleTaskController extends Controller $task = (new SingleTask()) ->setPerson($person) ->setAssignee($this->getUser()) + ->setType('task_default') ; $this->denyAccessUnlessGranted(TaskVoter::CREATE, $task, 'You are not ' @@ -67,13 +69,17 @@ class SingleTaskController extends Controller $form->handleRequest($request); - if ($form->isSubmitted() && $form->isValid()) { - $em = $this->getDoctrine()->getManager(); - $em->persist($task); - - $this->addFlash('success', "The task is created"); - - $em->flush(); + if ($form->isSubmitted()) { + if ($form->isValid()) { + $em = $this->getDoctrine()->getManager(); + $em->persist($task); + + $this->addFlash('success', "The task is created"); + + $em->flush(); + } else { + $this->addFlash('error', "This form contains errors"); + } } return $this->render('ChillTaskBundle:SingleTask:new.html.twig', array( @@ -90,7 +96,8 @@ class SingleTaskController extends Controller protected function createCreateForm(SingleTask $task) { $form = $this->createForm(SingleTaskType::class, $task, [ - 'center' => $task->getCenter() + 'center' => $task->getCenter(), + 'role' => new Role(TaskVoter::CREATE) ]); $form->add('submit', SubmitType::class); diff --git a/Entity/AbstractTask.php b/Entity/AbstractTask.php index 4b5df1093..0badeb83f 100644 --- a/Entity/AbstractTask.php +++ b/Entity/AbstractTask.php @@ -8,11 +8,17 @@ use Chill\PersonBundle\Entity\Person; use Chill\MainBundle\Entity\Scope; use Chill\MainBundle\Entity\HasScopeInterface; use Chill\MainBundle\Entity\HasCenterInterface; +use Symfony\Component\Validator\Constraints as Assert; +use Chill\MainBundle\Validator\Constraints\Entity\UserCircleConsistency; /** * AbstractTask * * @ORM\MappedSuperclass() + * @UserCircleConsistency( + * "CHILL_TASK_TASK_SHOW", + * getUserFunction="getAssignee" + * ) */ abstract class AbstractTask implements HasScopeInterface, HasCenterInterface { @@ -35,6 +41,7 @@ abstract class AbstractTask implements HasScopeInterface, HasCenterInterface * @var string * * @ORM\Column(name="title", type="text") + * @Assert\NotBlank() */ private $title = ''; @@ -60,6 +67,7 @@ abstract class AbstractTask implements HasScopeInterface, HasCenterInterface * @ORM\ManyToOne( * targetEntity="\Chill\PersonBundle\Entity\Person" * ) + * @Assert\NotNull() */ private $person; @@ -69,6 +77,7 @@ abstract class AbstractTask implements HasScopeInterface, HasCenterInterface * @ORM\ManyToOne( * targetEntity="\Chill\MainBundle\Entity\Scope" * ) + * @Assert\NotNull() */ private $circle; @@ -81,7 +90,7 @@ abstract class AbstractTask implements HasScopeInterface, HasCenterInterface */ public function setType($type) { - $this->type = $type; + $this->type = (string) $type; return $this; } @@ -129,7 +138,7 @@ abstract class AbstractTask implements HasScopeInterface, HasCenterInterface */ public function setTitle($title) { - $this->title = $title; + $this->title = (string) $title; return $this; } @@ -153,7 +162,7 @@ abstract class AbstractTask implements HasScopeInterface, HasCenterInterface */ public function setDescription($description) { - $this->description = $description; + $this->description = (string) $description; return $this; } diff --git a/Entity/SingleTask.php b/Entity/SingleTask.php index 77627906d..3a7861fdb 100644 --- a/Entity/SingleTask.php +++ b/Entity/SingleTask.php @@ -3,6 +3,7 @@ namespace Chill\TaskBundle\Entity; use Doctrine\ORM\Mapping as ORM; +use Symfony\Component\Validator\Constraints as Assert; /** * SingleTask @@ -25,6 +26,11 @@ class SingleTask extends AbstractTask * @var \DateTime * * @ORM\Column(name="start_date", type="date", nullable=true) + * @Assert\Date() + * @Assert\Expression( + * "value === null or value < this.getEndDate()", + * message="The start date must be before the end date" + * ) */ private $startDate; @@ -32,6 +38,7 @@ class SingleTask extends AbstractTask * @var \DateTime * * @ORM\Column(name="end_date", type="date", nullable=true) + * @Assert\Date() */ private $endDate; diff --git a/Form/SingleTaskType.php b/Form/SingleTaskType.php index 93ed932ba..b2c884496 100644 --- a/Form/SingleTaskType.php +++ b/Form/SingleTaskType.php @@ -20,6 +20,7 @@ namespace Chill\TaskBundle\Form; use Symfony\Component\Form\AbstractType; use Symfony\Component\Form\FormBuilderInterface; use Chill\MainBundle\Form\Type\ChillDateType; +use Chill\MainBundle\Entity\Center; use Symfony\Component\Form\Extension\Core\Type\TextType; use Symfony\Component\Form\Extension\Core\Type\TextareaType; use Chill\MainBundle\Form\Type\UserPickerType; @@ -28,6 +29,8 @@ use Symfony\Component\Form\Extension\Core\Type\ChoiceType; use Symfony\Component\Form\Extension\Core\Type\HiddenType; use Symfony\Component\OptionsResolver\OptionsResolver; use Symfony\Component\Security\Core\Role\Role; +use Chill\TaskBundle\Security\Authorization\TaskVoter; +use Chill\MainBundle\Form\Type\DateIntervalType; /** * @@ -46,11 +49,11 @@ class SingleTaskType extends AbstractType ->add('assignee', UserPickerType::class, [ 'required' => false, 'center' => $options['center'], - 'role' => new Role(\Chill\PersonBundle\Security\Authorization\PersonVoter::UPDATE) + 'role' => $options['role'] ]) ->add('circle', ScopePickerType::class, [ 'center' => $options['center'], - 'role' => new Role(\Chill\ActivityBundle\Security\Authorization\ActivityVoter::SEE) + 'role' => $options['role'] ]) ->add('startDate', ChillDateType::class, [ 'required' => false @@ -58,7 +61,7 @@ class SingleTaskType extends AbstractType ->add('endDate', ChillDateType::class, [ 'required' => false ]) - ->add('warningInterval', TextType::class, [ + ->add('warningInterval', DateIntervalType::class, [ 'required' => false ]) ; @@ -68,7 +71,9 @@ class SingleTaskType extends AbstractType { $resolver ->setRequired('center') - ->setAllowedTypes('center', [ \Chill\MainBundle\Entity\Center::class ]) + ->setAllowedTypes('center', [ Center::class ]) + ->setRequired('role') + ->setAllowedTypes('role', [ Role::class ]) ; } } diff --git a/Resources/translations/validators.fr.yml b/Resources/translations/validators.fr.yml new file mode 100644 index 000000000..c6c444163 --- /dev/null +++ b/Resources/translations/validators.fr.yml @@ -0,0 +1 @@ +The start date must be before the end date: La date de début doit être avant la date de fin \ No newline at end of file