1

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);
}
1
  • @JohnLobo I need it to be true. It's a public route. It needs to be true. If I set it to false then I have to authenticate a user or something. It's a publicly accessible route for customer's that gonna register. Commented Dec 11, 2021 at 5:50

3 Answers 3

7

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.

Sign up to request clarification or add additional context in comments.

Comments

2

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

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.