Laravel 4 验证被破坏
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18852536/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Laravel 4 Validation is Broken
提问by suncoastkid
A simple date format mm/dd/yyyy validation is all I need...
我只需要一个简单的日期格式 mm/dd/yyyy 验证...
$rules = array(
'renewal_date' => array('required', 'date_format:?')
);
What do I set the date format to? The Laravel documentation could be so much better.
我将日期格式设置为什么?Laravel 文档可能会好得多。
采纳答案by suncoastkid
Workaround:
解决方法:
'renewal_date' => array('required', 'date_format:m/d/Y', 'regex:/[0-9]{2}\/[0-9]{2}\/[0-9]{4}/')
回答by Antonio Carlos Ribeiro
Documentation is pretty clear to me, you should use
文档对我来说很清楚,你应该使用
date_format:format
日期格式:格式
"The field under validation must match the format defined according to the date_parse_from_formatPHP function."
“验证中的字段必须与根据date_parse_from_formatPHP 函数定义的格式相匹配。”
Looking at it: http://php.net/manual/en/function.date-parse-from-format.php, I see you can do something like this:
看着它:http://php.net/manual/en/function.date-parse-from-format.php,我看到你可以做这样的事情:
$rules = array(
'renewal_date' => array('required', 'date_format:"m/d/Y"')
);
This is pure PHP test for it:
这是它的纯 PHP 测试:
print_r(date_parse_from_format("m/d/Y", "04/01/2013"));
You can also do it manually in Laravel to test:
你也可以在 Laravel 中手动进行测试:
$v = Validator::make(['date' => '09/26/13'], ['date' => 'date_format:"m/d/Y"']);
var_dump( $v->passes() );
To me it's printing
对我来说是印刷
boolean true
布尔真
回答by mppfiles
I got a similar problem, but with a d/m/Y date format. In my case, the problem was that I defined both "date" and "date_format" rules for the same field:
我遇到了类似的问题,但使用的是 ad/m/Y 日期格式。就我而言,问题是我为同一字段定义了“date”和“date_format”规则:
public static $rules = array(
'birthday' => 'required|date|date_format:"d/m/Y"',
...
The solution is to remove the "date" validator: you must not use both. Like this:
解决方案是删除“日期”验证器:您不能同时使用两者。像这样:
public static $rules = array(
'birthday' => 'required|date_format:"d/m/Y"',
...
after that, all is well.
在那之后,一切都很好。
回答by Bora
You should use with double quote
like "Y-m-d H:i:s"
你应该使用double quote
像"Y-m-d H:i:s"
$rules = array(
'renewal_date' => array('required', 'date_format:"m/d/Y"')
^ ^ this ones
);
Discussion about this issue on GitHub:https://github.com/laravel/laravel/pull/1192
GitHub上关于这个问题的讨论:https : //github.com/laravel/laravel/pull/1192
回答by alejandro
date_format didn't work for me , so I did this custom validation
date_format 对我不起作用,所以我做了这个自定义验证
Validator::extend('customdate', function($attribute, $value, $parameters) {
$parsed_date = date_parse_from_format ( "Y-m-d" , $value);
$year = $parsed_date['year'];
$month = $parsed_date['month'];
$month = $month <= 9 ? "0" . $month : $month;
$day = $parsed_date['day'];
$day = $day <= 9 ? "0" . $day : $day;
return checkdate($month, $day, $year);
});
$validation = Validator::make(
array('date' => $num),
array('date' => 'customdate')
);
回答by mbarlund
Use the PHP date_parse_from_format
(Laravel 4):
使用 PHP date_parse_from_format
(Laravel 4):
'birthday' => 'date_format:m/d/Y'
Your validation message will also use "m/d/Y"
which the average user will not understand.
The birthday does not match the format m/d/Y
您的验证消息也将使用"m/d/Y"
普通用户无法理解的内容。
生日与格式 m/d/Y 不匹配
Recommend customizing your message for this invalid response. The birthday does not match the format mm/dd/yyyy
建议针对此无效响应自定义您的消息。 生日与格式 mm/dd/yyyy 不匹配