Validating a router {path} in Laravel
If you have a router like this, there may be times when you want to validate the {name} value. You can also do this using Route ->where here, but I just handle
GGAMI
Route::get('/{name}/info',If you have a router like this, there may be times when you want to validate the {name} value. You can also do this using Route ->where here, but I just handle
Route::get('/{name}/info',If you have a router like this, there may be times when you want to validate the {name} value.
You can also do this using Route ->where here, but I just handle validation in FormRequest.
class GetItemInfoRequest extends FormRequest
{
public function rules(): array
{
return [
'name' => ['required', Rule::in(Name::cases())],
];
}
public function authorize(): bool
{
return true;
}
// 이 아래 function 을 추가 해준다.
protected function prepareForValidation(): void
{
$this->merge([...$this->route()->parameters()]);
}
}Adding prepareForValidation allows you to handle the route path in the Request rules as well.
I've configured it so that only the Name Enum can be used to validate the path.