使用 Laravel 验证规则检查用户的年龄

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

Check user's age with laravel validation rules

validationlaravelmodel

提问by Mike

How can I check the age of a user upon registration? I want to set the minimum age to be 13 years old. I ask for the user's date of birth and when I validate the other credentials, I want to check that they are in fact 13+.

如何在注册时查看用户的年龄?我想将最低年龄设置为 13 岁。我询问用户的出生日期,当我验证其他凭据时,我想检查它们是否实际上是 13 岁以上。

An excerpt from my User model looks like this:

我的用户模型的摘录如下所示:

$rules = [
            'name'                  => 'required|alpha|min:1',
            'email'                 => 'required|email|unique:users',
            'dob'                   => 'required|date'
         ];

How can I check that the date entered is 13 years ago or more?

如何检查输入的日期是 13 年前或更早?

I have seen that I can use the before:yyy-mm-ddrule from the Laravel Docs, like so:

我已经看到我可以使用Laravel Docs 中before:yyy-mm-dd规则,如下所示:

$rules = [
            'name'                  => 'required|alpha|min:1',
            'email'                 => 'required|email|unique:users',
            'dob'                   => 'required|date|before:2001-04-15'
         ];
  1. How do I calculate the value?
  2. How do I use that value within the rules?
  1. 我如何计算价值?
  2. 我如何在规则中使用该值?

回答by Zanshin13

A simple way to check that the date is greater(older) than N years is to set the beforerule to minus Nyears.

检查日期是否大于(旧)N 年的一种简单方法是将before规则设置为减去 N年。

$rules = [
    'dob' => 'required|date|before:-13 years',
]

回答by Jarek Tkaczyk

RMcLeod answer is OK, but I'd suggest you extracting this as a custom rule:

RMcLeod 答案是可以的,但我建议您将其提取为自定义规则:

Validator::extend('olderThan', function($attribute, $value, $parameters)
{
    $minAge = ( ! empty($parameters)) ? (int) $parameters[0] : 13;
    return (new DateTime)->diff(new DateTime($value))->y >= $minAge;

    // or the same using Carbon:
    // return Carbon\Carbon::now()->diff(new Carbon\Carbon($value))->y >= $minAge;
});

This way you can use the rule for any age you like:

通过这种方式,您可以将规则用于您喜欢的任何年龄:

$rules = ['dob' => 'olderThan']; // checks for 13 years as a default age
$rules = ['dob' => 'olderThan:15']; // checks for 15 years etc

回答by RMcLeod

You can use Carbon which comes with laravel

您可以使用 laravel 附带的 Carbon

$dt = new Carbon\Carbon();
$before = $dt->subYears(13)->format('Y-m-d');

$rules = [
    ...
    'dob' => 'required|date|before:' . $before
];

回答by Appocrypha

This is a bit old, but I want to share the way I do it.

这有点旧,但我想分享我的做法。

 public function rules()
{
    return 
    [
        ...
        'age' => 'required|date|before_or_equal:'.\Carbon\Carbon::now()->subYears(18)->format('Y-m-d'),
        ...
    ];
}

Using beforeis nice but it's a bit ugly for the end user, because if today it's his birthday, he won't be able to pass. With before_or_equalyou get the perfect behaviour. A way to improve this would be checking the timezone with Carbon if you target a worldwide audience.

使用before是好的,但对最终用户来说有点难看,因为如果今天是他的生日,他将无法通过。有了before_or_equal你,你就会得到完美的行为。如果您的目标是全球受众,则改善这一点的一种方法是使用 Carbon 检查时区。