Laravel until today rules ... how to do it

For this rule, I am getting the error syntax error, unexpected '.', expecting ')'

public static $rules = array(
    'first_name' => 'required|alpha-dash',
    'last_name' => ' required|alpha-dash',
    'media_release' => 'required|boolean',
    'birthday' => 'before:' . date('Y-m-d')

);

      

I can't figure out why this won't work. I am running Laravel 4.2.12

+3


source to share


4 answers


You cannot use a function when defining member variables of a class. You will have to move this part to your constructor:

<?php
class Foo {
    public static $rules = array(
        'first_name' => 'required|alpha-dash',
        'last_name' => ' required|alpha-dash',
        'media_release' => 'required|boolean'
    );
    public function __construct()
    {
        self::$rules['birthday'] = 'before:' . date('Y-m-d');
    }

      

EDIT:



The above solution may not work in Laravel. You may need to use the "Custom Validator":

http://laravel.com/docs/4.2/validation#custom-validation-rules

+1


source


try this:

'birthday' => 'date_format:Y-m-d|before:today',

      



goodbye

+9


source


For future users only:

Laravel 5 using form requests:

'birthday' => 'before:today',    // Doesn't accept today date
'birthday' => 'before:tomorrow', // Accept today date

      

+5


source


I recommend that you add the operator before

and after

for birthdays .

'birthday' => date_format:Y-m-d|before:today|after:1900-01-01

      

You will avoid records such as 1111-1-1

because no one was born after 1900.

+1


source







All Articles