Yud*_*ama 5 validation laravel
这是我的表单请求代码,我想在验证成功后添加新变量,这样我就可以在我的控制器上访问该变量:
class CouponRequest extends Request
{
/**
* Get the validation rules that apply to the request.
*
* @return array
*/
public function rules()
{
return [
'start_year' => 'required',
'start_month' => 'required',
'start_day' => 'required',
'start_time' => 'required',
'finish_year' => 'required',
'finish_month' => 'required',
'finish_day' => 'required',
'finish_time' => 'required',
];
}
public function afterValidation()
{
$this->start_date = Carbon::create( $this->start_year, $this->start_month, $this->start_day );
}
}
Run Code Online (Sandbox Code Playgroud)
所以在验证没有错误后,我可以在我的控制器上调用这个实例:
$request->start_date;
Run Code Online (Sandbox Code Playgroud)
我可以这样做吗?
Chi*_*sad 10
上述所有方法都有效,但在我看来,我会重写passedValidation表单请求类中的方法。通过验证检查后调用此方法,从而保持数据干净。
前任。
public function passedValidation()
{
$this->merge([
'start_date' => Carbon::create( $this->start_year, $this->start_month, $this->start_day )
]);
}
Run Code Online (Sandbox Code Playgroud)
如果您现在转储数据,您还应该看到新的 start_date 值。
小智 0
我在控制器中进行了验证。该方法有一个“Request $request”参数。我有一个我这样做:
$input = $request->all();
$input['my_new_field] = 'the_data';
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
5803 次 |
| 最近记录: |