I've created a custom form request with php aritisan make:request, I have added in validation rules:
public function rules()
{
return [
'first_name' => 'required|string',
'last_name' => 'required|string',
...
'email' => 'email|unique:auth_user,email',
'password' => ['required', 'confirmed', Password::min(8)],
'password_confirmation' => 'required|string|min:8',
];
}
However, it redirects me to the index page. This is my function:
public function store(RegistrationFormRequest $request)
{
return dd($request);
}
My POST request doesn't actually have any parameters, so I already assumed that it wont reach the dd function but instead send a 422 error as it fails the validation. But it doesn't, instead it redirects me to the / page on my web.php routes.
Here's the structure:
api.php:
Route::post('auths/register', [UserController::class, 'store']);
Controller:
public function store(RegistrationFormRequest $request)
{
return dd($request);
}
Custom form request class:
class RegistrationFormRequest extends FormRequest
{
/**
* Determine if the user is authorized to make this request.
*
* @return bool
*/
public function authorize()
{
return true;
}
/**
* Get the validation rules that apply to the request.
*
* @return array
*/
public function rules()
{
return [
'first_name' => 'required|string',
'last_name' => 'required|string',
'middle_name' => 'required|string',
'phone' => 'required|numeric|size:10',
'region' => 'exists:base_addressoption,name',
'province' => 'exists:base_addressoption,name',
'city' => 'exists:base_addressoption,name',
'brgy' => 'exists:base_addressoption,name',
'street' => 'required|string',
'address1' => 'required|string',
'zip_code' => 'required|numeric',
'address2' => 'nullable|string',
'email' => 'email|unique:auth_user,email',
'password' => ['required', 'confirmed', Password::min(8)],
'password_confirmation' => 'required|string|min:8',
];
}
}
It works when I add in all the parameters, but that I need to validate when a parameter isn't present. It needs to return an error 422.
More info. Using the regular Request works but not when using my own form request
public function store(Request $request)
{
return dd($request);
}