Answers
Firstly, you can not validate route parameters inside Form Request
But, you can use regex for validating your route Example:
Route::get('/calendar/{day}', 'App\Http\Controllers\HomeController@calendar')
->name('calendar')
->where('day', '/^[0-9]{4}(0[1-9]|1[0-2])(0[1-9]|[1-2][0-9]|3[0-1])$/');
If You still want to use validate()
function or Form Request
$request->merge([
'day' => $day
]);
$this->validate($request, [
'day' => 'date_format:Ymd',
]);
Form Request Way - Override all()
method
public function all($k = null){
$data = parent::all($k);
$data['day'] = $this->route('day');
return $data;
}
If Nothing Works, Try Following code
public function calendar(Request $request, $day){
$data = $request->all();
$data['day'] = $day;
$validator = Validator::make($data, [
'day' => 'required|date_format:Ymd',
]);
if($validator->fails()){
// Do Something abort(404);
}
}
0 comments:
Post a Comment
Thanks