使用 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
Check user's age with laravel validation rules
提问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-dd
rule 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'
];
- How do I calculate the value?
- How do I use that value within the rules?
- 我如何计算价值?
- 我如何在规则中使用该值?
回答by Zanshin13
A simple way to check that the date is greater(older) than N years is to set the before
rule 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 before
is 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_equal
you 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 检查时区。