rawac41707
rawac41707

Reputation: 143

Laravel custom form request redirecting to index page

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);
}

Upvotes: 1

Views: 1580

Answers (3)

Amit Nadiyapara
Amit Nadiyapara

Reputation: 246

If you are testing in Postman, please verify that you have set the headers correctly:

Accept: application/json
Content-Type: application/json

Ensure that both headers are included in your request. Sometimes, missing or incorrect headers can cause unexpected issues with the API response.

Upvotes: 0

Marco Vieira
Marco Vieira

Reputation: 21

Im using Postman to send posts requests to my api in laravel and all i need is to put X-Requested-With: XMLHttpRequest in Headers to prevent redirections

Upvotes: 2

rawac41707
rawac41707

Reputation: 143

I found the answer later on. It's an API route request, but Laravel behaves as if it's a regular web route.

So if you're testing in Postman, add X-Requested-With: XMLHttpRequest in the header so Laravel knows its an XHR request.

Upvotes: 7

Related Questions