Created
July 16, 2013 08:13
-
-
Save moux2003/6006787 to your computer and use it in GitHub Desktop.
Validate user age
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| use FOS\UserBundle\Model\User as ModelUser; | |
| use Virgin\UserBundle\Validator\Constraints as VirginAssert; | |
| /** | |
| * Storage agnostic user object | |
| * | |
| * @author Thibault Duplessis <[email protected]> | |
| * @author Johannes M. Schmitt <[email protected]> | |
| */ | |
| class User extends ModelUser | |
| { | |
| /** | |
| * @var string | |
| */ | |
| protected $firstname; | |
| /** | |
| * @var string | |
| */ | |
| protected $lastname; | |
| /** | |
| * @var string | |
| */ | |
| protected $title; | |
| /** | |
| * @var \DateTime | |
| * @Assert\NotBlank(message="security.user.dateOfBirth.not.blank", | |
| * groups={"generalPublic"} | |
| * ) | |
| * @VirginAssert\OverAge(age="18") | |
| */ | |
| protected $dateOfBirth; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| namespace Virgin\UserBundle\Validator\Constraints; | |
| use Symfony\Component\Validator\Constraint; | |
| /** | |
| * @Annotation | |
| * Class OverAge | |
| * @package Virgin\UserBundle\Validator\Constraints | |
| */ | |
| class OverAge extends Constraint | |
| { | |
| public $message = "L'âge minimum requis est %age% ans."; | |
| public $age = 18; | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| namespace Virgin\UserBundle\Validator\Constraints; | |
| use Symfony\Component\Validator\Constraint; | |
| use Symfony\Component\Validator\ConstraintValidator; | |
| use Symfony\Component\Validator\Exception\UnexpectedTypeException; | |
| /** | |
| * Validate the age given a limit | |
| * @api | |
| */ | |
| class OverAgeValidator extends ConstraintValidator | |
| { | |
| public function validate($value, Constraint $constraint) | |
| { | |
| if (!$value instanceof \DateTime) { | |
| throw new UnexpectedTypeException($value, 'DateTime'); | |
| } | |
| $minimumDate = new \DateTime(date('Y-m-d')); | |
| $minimumDate->sub(new \DateInterval('P' . $constraint->age . 'Y')); | |
| $interval = $minimumDate->diff($value); | |
| $dayDiff = $interval->format('%R%d'); | |
| if ($dayDiff > 0) { | |
| $this->context->addViolation($constraint->message, array('%age%' => $constraint->age)); | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment