Andy Holmes
Andy Holmes

Reputation: 8077

Laravel 5.8 Custom Form Request not working as intended?

I created a custom form request named ClientStoreRequest with the following code:

<?php

namespace App\Http\Requests;

use Illuminate\Foundation\Http\FormRequest;

class ClientStoreRequest extends FormRequest
{
    /**
     * Determine if the user is authorized to make this request.
     *
     * @return bool
     */
    public function authorize()
    {
        return true;
        // return  $this->user()->can('add-clients');
    }

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array
     */
    public function rules()
    {
        return [
            'name' => 'required|unique:clients|max:255',
            'website' => 'required|url',
            'street' => 'required',
            'town' => 'required',
            'postcode' => 'required|max:8',
            'county' => 'required',
            'country' => 'required'
        ];
    }
}

I then modified my ClientController's store method to look like this:

    /**
     * Store a newly created resource in storage.
     *
     * @param  ClientStoreRequest  $request
     * @return \Illuminate\Http\Response
     */
    public function store(ClientStoreRequest $request)
    {
        dd(1);
    }

So when the form is submitted, it should kill the page and print a 1 to the screen. When I use ClientStoreRequest $request, it just sends me back to the page where I submitted the form with no errors and no dd(1) result, however when I utilise Request $request it prints 1 to the screen.

Am I missing something really obvious? I've followed the docs to make this.

Edit: I'm using a resource controller so the route is Route::resource('clients', 'ClientController');

Upvotes: 5

Views: 3526

Answers (1)

Andy Holmes
Andy Holmes

Reputation: 8077

A bit embarrassing but this one was purely down to developer error. The custom form request is actually working correctly... just my rules are referencing one field that I forgot to put into my form so isn't showing the errors to the screen! Once I echoed the usual $errors array I could see my error - I'll blame the late night coding!

Upvotes: 3

Related Questions