2017 © Pedro Peláez
 

symfony-bundle date-extra-validator-bundle

Symfony2 Date Validator for Minimum and Maximum Constraints

image

oh/date-extra-validator-bundle

Symfony2 Date Validator for Minimum and Maximum Constraints

  • Thursday, August 29, 2013
  • by ollietb
  • Repository
  • 2 Watchers
  • 4 Stars
  • 36,806 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 2 Forks
  • 0 Open issues
  • 1 Versions
  • 13 % Grown

The README.md

OhDateExtraValidatorBundle

A minimum and maximum date validator for Symfony2, (*1)

Installation

Install this bundle as usual by adding to composer.json:, (*2)

"oh/date-extra-validator-bundle": "dev-master"

Register the bundle in app/AppKernel.php:, (*3)

// app/AppKernel.php
public function registerBundles()
{
    return array(
        // ...
        new Oh\DateExtraValidatorBundle\OhDateExtraValidatorBundle(),
    );
}

Usage

Add the constraints to your model

...
use Oh\DateExtraValidatorBundle\Validator\Constraints as OhAssert;

/**
 * @ORM\Entity()
 */
class Event
{
    /**
     * @ORM\Column(type="datetime") 
     * 
     * @OhAssert\DateExtra(min="-1 year", max="+1 year", intlTimeFormat=\IntlDateFormatter::NONE)
     */
    protected $start_time;
}

Or in your yml (WARNING! untested - I use annotations), (*4)

Acme\DemoBundle\Entity\AcmeEntity:
    properties:
        start_time:
            - Oh\DateExtraValidatorBundle\Validator\Constraints\DateExtra: {min="-1 year", max="+1 year", intlTimeFormat=\IntlDateFormatter::NONE}

Options

Messages

When you validate your model through a form you should see the error messages as defined in Oh\DateExtraValidatorBundle\Validator\Constraints\DateExtra., (*5)

public $minMessage = 'You cannot choose a date before {{ min }}.';
public $maxMessage = 'You cannot choose a date after {{ max }}.';
public $invalidMessage = 'The date is invalid';
...

Each message can use {{ min }}, {{ max }} and {{ value }} so you can for example put:, (*6)

/**
 * @OhAssert\DateExtra(minMessage="The date you supplied, {{ value }}, should be between {{ min }} and {{ max }}", min="-1 year", max="+1 year")
 */

The format of the date in the error messages can be a normal date string or an IntlDateFormatter date., (*7)

/**
 * @OhAssert\DateExtra(format="Y-m-d",min="-1 year", max="+1 year")
 * or
 * @OhAssert\DateExtra(intlDateFormat=\IntlDateFormatter::LONG, intlTimeFormat=\IntlDateFormatter::LONG, min="-1 year", max="+1 year")
 */

For the UK timezone Europe/London the 2 examples above would output "You cannot choose a date before 2011-09-04." and "You cannot choose a date before September 4, 2011 8:10:34 PM GMT+01:00.", (*8)

You can also set the timezone manually by specifying it in the constructor (eg timezone="Europe/London"), (*9)

Valid values

The class can handle most date formats; \DateTime, array('year'=>2012,'month'=>9,'day'=>4), unix timestamp (eg 1346789811), string (eg 2012-09-04) or an object that returns one of these values in __toString(), (*10)

Tests

Build Status, (*11)

Credits

  • Ollie Harridge ollietb as main author.

The Versions

29/08 2013

dev-master

9999999-dev https://github.com/ollieLtd/OhDateExtraValidatorBundle

Symfony2 Date Validator for Minimum and Maximum Constraints

  Sources   Download

The Requires

 

by Ollie Harridge

validator symfony2 bundle date