laravel - 验证年龄变量 - Laravel RegisterController
问题描述
我在 Laravel OOB 注册表单中添加了 3 个字段,它们是出生月份、日期和年份。我将这些字段传递给 RegisterController 中的验证器函数,并使用 Carbon 将它们转换为年龄:
$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'
我已经研究了After Validation Hook,但不明白如何在我的情况下使用它。
解决方案
您可以将 $theAge 变量添加到数据数组中。
$data['age'] = Carbon::createFromDate($data['birthyear'], $data['birthmonth'], $data['birthday'])->age;
推荐阅读
- python - 加载个人数据集时出现致命错误
- csv - 如何在 SuiteScript 中从 FileCabinet 加载文件
- python - 如何在 Keras 中计算和求和多个小批量的梯度?
- google-drive-api - 如何在 vlc 中从谷歌驱动器流式传输大尺寸视频
- python - 用正则表达式替换 Pandas 中 None 单元格中的值
- android - 我如何将此数组编译成 Kotlin 中的列表视图?
- python - 如何从 GCP 存储桶中读取 Apache Beam 中的多个文件
- c# - 泛型方法重载机制
- java - 如何使用服务发现重写 Spring Cloud Gateway 的 serviceId
- azure - Application Insights 日志:按信息内容搜索