Laravel 4 Validation is Broken
Solution 1
Workaround:
'renewal_date' => array('required', 'date_format:m/d/Y', 'regex:/[0-9]{2}\/[0-9]{2}\/[0-9]{4}/')
Solution 2
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_format PHP function."
Looking at it: http://php.net/manual/en/function.date-parse-from-format.php, I see you can do something like this:
$rules = array(
'renewal_date' => array('required', 'date_format:"m/d/Y"')
);
This is pure PHP test for it:
print_r(date_parse_from_format("m/d/Y", "04/01/2013"));
You can also do it manually in Laravel to test:
$v = Validator::make(['date' => '09/26/13'], ['date' => 'date_format:"m/d/Y"']);
var_dump( $v->passes() );
To me it's printing
boolean true
Solution 3
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:
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.
Solution 4
You should use with double quote
like "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
suncoastkid
Full-stack web developer; advertising, marketing & SEM expert.
Updated on July 28, 2022Comments
-
suncoastkid almost 2 years
A simple date format mm/dd/yyyy validation is all I need...
$rules = array( 'renewal_date' => array('required', 'date_format:?') );
What do I set the date format to? The Laravel documentation could be so much better.
-
suncoastkid over 10 years'renewal_date' => array('required', 'date_format:"m/d/Y"') still does not work.
-
Bora over 10 years@suncoastkid I guess syntax is wrong. Try without
required
like$rules = array('renewal_date' => 'date_format:"m/d/Y"');
-
suncoastkid over 10 years'renewal_date' => array('required', 'date_format:"m/d/Y"') does not work
-
suncoastkid over 10 yearsThanks but that still doesn't work... this is so frustrating. This should be simple.
-
Antonio Carlos Ribeiro over 10 yearsAdded a standalone test so you can see if it's Laravel or anything else in your code.
-
Antonio Carlos Ribeiro over 10 yearsThere is indeed a problem with it, some warnings are not being processed. Just posted an issue on github: github.com/laravel/laravel/issues/2317.
-
suncoastkid over 10 yearsWorkaround: 'renewal_date' => array('required', 'date_format:m/d/Y', 'regex:/[0-9]{2}\/[0-9]{2}\/[0-9]{4}/')
-
Joseph Silber over 10 years@AntonioCarlosRibeiro - How do you explain this? sandbox.onlinephpfunctions.com/code/…
-
Antonio Carlos Ribeiro over 10 yearsI can`t see anything wrong. That's the normal behaviour of this function.
-
Antonio Carlos Ribeiro over 10 yearsSorry, it's a PHP bug, just answered it on github.