php Laravel 在今天之前统治...怎么办

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27624559/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 23:26:30  来源:igfitidea点击:

Laravel before today rule... how to do

phplaravel

提问by Phil

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

对于这条规则,我收到一个错误 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'm running Laravel 4.2.12

我不明白为什么这行不通。我正在运行 Laravel 4.2.12

采纳答案by mopo922

You can't use a function when defining class member variables. You'll have to move that 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 have to use a "Custom Validator" instead:

上述解决方案在 Laravel 中可能不起作用。您可能必须改用“自定义验证器”:

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

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

UPDATE:

更新:

Looks like Laravel 5 introduced a better solution. See the answer by Fernando, below.

看起来 Laravel 5 引入了更好的解决方案。请参阅下面 Fernando 的回答。

回答by Fernando lugo

try this:

尝试这个:

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

bye

再见

回答by lmarcelocc

Just for future users:

仅供未来用户使用:

Laravel 5 using form requests:

Laravel 5 使用表单请求:

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

回答by António Almeida

I recommend you to add a beforeand an afterstatement for birthdays.

我建议你为生日添加一个before和一个after声明。

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

You'll avoid entries like 1111-1-1because no one alive was born before the year 1900.

您将避免出现像1111-1-1因为 1900 年之前没有人出生这样的条目。