Added validation for end date before start date (#167)

* added validation for end date before start date
* added unit test for new validations
This commit is contained in:
Kevin Papst
2018-06-20 23:27:56 +02:00
committed by GitHub
parent 071abc8fc8
commit 8c686b0714
5 changed files with 158 additions and 0 deletions

View File

@@ -12,6 +12,7 @@ namespace App\Entity;
use App\Entity\User;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;
use Symfony\Component\Validator\Context\ExecutionContextInterface;
/**
* Timesheet entity.
@@ -57,6 +58,7 @@ class Timesheet
* @var integer
*
* @ORM\Column(name="duration", type="integer", nullable=true)
* @Assert\GreaterThanOrEqual(0)
*/
private $duration = 0;
@@ -252,4 +254,20 @@ class Timesheet
{
return $this->rate;
}
/**
* @param ExecutionContextInterface $context
* @param mixed $payload
*
* @Assert\Callback
*/
public function validate(ExecutionContextInterface $context, $payload)
{
if ($this->getEnd() !== null && $this->getEnd()->getTimestamp() < $this->getBegin()->getTimestamp()) {
$context->buildViolation('End date must not be earlier then start date.')
->atPath('end')
->setTranslationDomain('validators')
->addViolation();
}
}
}