oh /date-extra-validator-bundle
此包最新版本(dev-master)没有可用的许可证信息。
Symfony2 日期验证器,用于最小和最大约束
dev-master
2013-08-29 12:01 UTC
Requires
- php: >=5.3.3
- symfony/form: *
- symfony/framework-bundle: *
- symfony/validator: *
This package is not auto-updated.
Last update: 2024-09-14 13:10:00 UTC
README
Symfony2 的最小和最大日期验证器
安装
通过在 composer.json 中添加来像通常一样安装此包
"oh/date-extra-validator-bundle": "dev-master"
在 app/AppKernel.php
中注册包
// app/AppKernel.php
public function registerBundles()
{
return array(
// ...
new Oh\DateExtraValidatorBundle\OhDateExtraValidatorBundle(),
);
}
用法
将约束添加到您的模型中
...
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;
}
或在您的 yml 中(警告!未测试 - 我使用注解)
Acme\DemoBundle\Entity\AcmeEntity:
properties:
start_time:
- Oh\DateExtraValidatorBundle\Validator\Constraints\DateExtra: {min="-1 year", max="+1 year", intlTimeFormat=\IntlDateFormatter::NONE}
选项
消息
当您通过表单验证模型时,您应该看到 Oh\DateExtraValidatorBundle\Validator\Constraints\DateExtra 中定义的错误消息。
public $minMessage = 'You cannot choose a date before {{ min }}.';
public $maxMessage = 'You cannot choose a date after {{ max }}.';
public $invalidMessage = 'The date is invalid';
...
每个消息都可以使用 {{ min }}
、{{ max }}
和 {{ value }}
,例如您可以放置
/**
* @OhAssert\DateExtra(minMessage="The date you supplied, {{ value }}, should be between {{ min }} and {{ max }}", min="-1 year", max="+1 year")
*/
错误消息中日期的格式可以是正常的日期字符串或一个 IntlDateFormatter 日期。
/**
* @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")
*/
对于英国时区 Europe/London,上述两个示例将输出 "您不能选择在 2011-09-04 之前的日期。" 和 "您不能选择在 2011 年 9 月 4 日下午 8:10:34 GMT+01:00 之前的日期。"
您也可以通过在构造函数中指定来手动设置时区(例如 timezone="Europe/London"
)
有效值
此类可以处理大多数日期格式;\DateTime
、array('year'=>2012,'month'=>9,'day'=>4)
、Unix 时间戳(例如 1346789811
)、字符串(例如 2012-09-04)或一个返回这些值之一的对象(__toString())
测试
致谢
- Ollie Harridge ollietb 作为主要作者。