简体   繁体   English

通过“尊重验证”进行月份验证

[英]Month validation with “Respect Validation”

I am trying to write validation rule for credit card month and I am using Respect Validation library for this. 我正在尝试为信用卡月份编写验证规则,并且为此使用了“ 尊重验证”库。

v::string()->date('m')->validate('02');

Result is FALSE but it must be TRUE because "02" is valid month 结果为FALSE但必须为TRUE因为“ 02”是有效月份

EDIT: Okay i found problem. 编辑:好的,我发现了问题。 Because February have only 28 days you can't verify date with this library without day (right now is 30th day in month, so it returns 03 instead of 02 in comparison). 由于2月只有28天,因此您无法没有日期就无法使用此库来验证日期(现在是每月30号,因此它返回03而不是02)。

Soloution: Soloution:

v::int()->between(1, 12)->validate(02);

OR 要么

you can add first day to comparison. 您可以添加第一天进行比较。

$value = '01-'.$input;    
v::string()->date('d-m')->validate($value);

You need to specify the format as !m . 您需要将format指定为!m As noted on PHP manual , if other parts of a date format are omitted the current time is used. 如PHP手册所述 ,如果省略日期格式的其他部分,则使用当前时间。 Using ! 使用! resets the time to UNIX epoch. 将时间重置为UNIX纪元。

This was explained on another answer here . 这在这里的另一个答案中得到了解释。

PS: we will try to fix that on the library itself. PS:我们将尝试在库本身上修复该问题。

The problem was that Validation was doing this verification using DateTime::createFromFormat() . 问题是Validation正在使用DateTime::createFromFormat()进行此验证。

The PHP manual says: PHP手册说:

If format does not contain the character ! 如果format不包含字符 then portions of the generated time which are not specified in format will be set to the current system time. 则未按格式指定的生成时间部分将设置为当前系统时间。

So, to avoid problems, a fix has been made and Validation is now using date_parse_from_format() instead. 因此,为避免出现问题, 已进行了修复 ,现在使用date_parse_from_format() 进行验证。

This error has been fixed on Mar 31th, 2016 (versions 0.8.14, 0.9.8 and 1.0.5) and it's available on 1.1 as well. 此错误已于2016年3月31日修复(版本0.8.14、0.9.8和1.0.5),并且在版本1.1中也可用。

You might consider to upgrade your version of the library. 您可能考虑升级库的版本。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM