Benjamin
Benjamin

Reputation: 1104

Is there a way in form vaildation to check if a given date is in a certain range?

I got a field in my form were the user can put in his birth date. I would like to check (via the isValid()-method) wether this date is in a certain range. Is there a way to do that via the symfony build in functionalities (like as an annototation)? I thought about using @Assert\Regex() but i this would be quite a struggle i guess ..

Upvotes: 0

Views: 46

Answers (1)

Cesar
Cesar

Reputation: 4116

If you are using Symfony 2.6+ you can use the Range Validator

Copied from the docs:

// src/AppBundle/Entity/Event.php
namespace AppBundle\Entity;

use Symfony\Component\Validator\Constraints as Assert;

class Event
{
    /**
     * @Assert\Range(
     *      min = "first day of January",
     *      max = "first day of January next year"
     * )
     */
    protected $startDate;
}

The minimum and maximum date of the range should be given as any date string accepted by the DateTime constructor

Upvotes: 1

Related Questions