Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel 4 Validation is Broken

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.

like image 601
suncoastkid Avatar asked Sep 17 '13 14:09

suncoastkid


2 Answers

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

like image 196
Antonio Carlos Ribeiro Avatar answered Oct 07 '22 23:10

Antonio Carlos Ribeiro


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.

like image 21
mppfiles Avatar answered Oct 07 '22 21:10

mppfiles