验证年龄变量-Laravel RegisterController
我在Laravel OOB登记表中添加了3个字段,分别是出生月份、出生日期和出生年份。我将这些字段传递给RegisterController中的validator函数,并将它们转换为一个带有碳的年龄:验证年龄变量-Laravel RegisterController,laravel,Laravel,我在Laravel OOB登记表中添加了3个字段,分别是出生月份、出生日期和出生年份。我将这些字段传递给RegisterController中的validator函数,并将它们转换为一个带有碳的年龄: $theAge = Carbon::createFromDate($data['birthyear'], $data['birthmonth'], $data['birthday'])->age; 这部分工作正常,我可以将变量传递到表中的字段,并查看正确的年龄 如何将$theAge添加到验
$theAge = Carbon::createFromDate($data['birthyear'], $data['birthmonth'], $data['birthday'])->age;
这部分工作正常,我可以将变量传递到表中的字段,并查看正确的年龄
如何将$theAge添加到验证器
return Validator::make($data, [
'email' => 'required|string|email|max:255|unique:users|confirmed',
'password' => 'required|string|min:8|confirmed',
'first_name' => 'required|string|max:255',
'last_name' => 'required|string|max:255',
'address' => 'required|string|max:255',
'city' => 'required|string|max:255',
'state' => 'required|string|max:2',
'zipcode' => 'required|string|max:10',
'brand' => 'required',
'opt_in' => 'required',
'g-recaptcha-response' => 'required|captcha',
'birthmonth' => 'required',
'birthday' => 'required',
'birthyear' => 'required',
]);
我尝试了以下操作,但在验证时似乎被忽略:
$theAge => 'bail|min:21'
我已经研究了,但不知道如何在我的情况下使用它。在调用验证器之前,您可以将计算出的值放回
$data
:
$theAge = Carbon::createFromDate($data['birthyear'], $data['birthmonth'], $data['birthday'])->age;
$data['age'] = $theAge;
return Validator::make($data, [
'email' => 'required|string|email|max:255|unique:users|confirmed',
'password' => 'required|string|min:8|confirmed',
'first_name' => 'required|string|max:255',
'last_name' => 'required|string|max:255',
'address' => 'required|string|max:255',
'city' => 'required|string|max:255',
'state' => 'required|string|max:2',
'zipcode' => 'required|string|max:10',
'brand' => 'required',
'opt_in' => 'required',
'g-recaptcha-response' => 'required|captcha',
'birthmonth' => 'required',
'birthday' => 'required',
'birthyear' => 'required',
'age' => 'min:21'
]);
<input name="birthday" type="date">
或者,您可以让用户使用日期选择器(例如)选择其出生日期,如下所示:
$theAge = Carbon::createFromDate($data['birthyear'], $data['birthmonth'], $data['birthday'])->age;
$data['age'] = $theAge;
return Validator::make($data, [
'email' => 'required|string|email|max:255|unique:users|confirmed',
'password' => 'required|string|min:8|confirmed',
'first_name' => 'required|string|max:255',
'last_name' => 'required|string|max:255',
'address' => 'required|string|max:255',
'city' => 'required|string|max:255',
'state' => 'required|string|max:2',
'zipcode' => 'required|string|max:10',
'brand' => 'required',
'opt_in' => 'required',
'g-recaptcha-response' => 'required|captcha',
'birthmonth' => 'required',
'birthday' => 'required',
'birthyear' => 'required',
'age' => 'min:21'
]);
<input name="birthday" type="date">
在调用验证器之前,可以将计算出的值放回
$data
,如下所示:
$theAge = Carbon::createFromDate($data['birthyear'], $data['birthmonth'], $data['birthday'])->age;
$data['age'] = $theAge;
return Validator::make($data, [
'email' => 'required|string|email|max:255|unique:users|confirmed',
'password' => 'required|string|min:8|confirmed',
'first_name' => 'required|string|max:255',
'last_name' => 'required|string|max:255',
'address' => 'required|string|max:255',
'city' => 'required|string|max:255',
'state' => 'required|string|max:2',
'zipcode' => 'required|string|max:10',
'brand' => 'required',
'opt_in' => 'required',
'g-recaptcha-response' => 'required|captcha',
'birthmonth' => 'required',
'birthday' => 'required',
'birthyear' => 'required',
'age' => 'min:21'
]);
<input name="birthday" type="date">
或者,您可以让用户使用日期选择器(例如)选择其出生日期,如下所示:
$theAge = Carbon::createFromDate($data['birthyear'], $data['birthmonth'], $data['birthday'])->age;
$data['age'] = $theAge;
return Validator::make($data, [
'email' => 'required|string|email|max:255|unique:users|confirmed',
'password' => 'required|string|min:8|confirmed',
'first_name' => 'required|string|max:255',
'last_name' => 'required|string|max:255',
'address' => 'required|string|max:255',
'city' => 'required|string|max:255',
'state' => 'required|string|max:2',
'zipcode' => 'required|string|max:10',
'brand' => 'required',
'opt_in' => 'required',
'g-recaptcha-response' => 'required|captcha',
'birthmonth' => 'required',
'birthday' => 'required',
'birthyear' => 'required',
'age' => 'min:21'
]);
<input name="birthday" type="date">
您可以将$theAge变量添加到数据数组中
$data['age'] = Carbon::createFromDate($data['birthyear'], $data['birthmonth'], $data['birthday'])->age;
您可以将$theAge变量添加到数据数组中
$data['age'] = Carbon::createFromDate($data['birthyear'], $data['birthmonth'], $data['birthday'])->age;
$data
变量中有什么,可以发布吗?$data是表单请求中的一个值数组。受保护的函数验证器(数组$data)您的$data
变量中有什么,可以发布吗?$data是表单请求中的一个值数组。受保护的函数验证器(数组$data)此方法在这种情况下很有用:$request->merge(['age'=>$theAge])
此方法在这种情况下很有用:$request->merge(['age'=>$theAge])