add validation to single task controller::new

This commit is contained in:
Julien Fastré 2018-04-17 11:39:16 +02:00
parent a02b9edc45
commit fd8b6490d0
5 changed files with 44 additions and 15 deletions

View File

@ -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);

View File

@ -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;
}

View File

@ -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;

View File

@ -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 ])
;
}
}

View File

@ -0,0 +1 @@
The start date must be before the end date: La date de début doit être avant la date de fin