5

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

Andy Holmes
  • 7,817
  • 10
  • 50
  • 83
  • How is your route set? – Techno Jul 13 '19 at 21:42
  • https://laravel.com/docs/5.8/validation#form-request-validation >> When you define rules for your request, your controller function will not be touched as it is not allowed to run it due to your rules. Do you send all the required parameters? – Techno Jul 13 '19 at 21:46
  • @RobBiermann I'm not sure what you mean, I'll post the route now – Andy Holmes Jul 13 '19 at 22:08

1 Answers1

3

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!

Andy Holmes
  • 7,817
  • 10
  • 50
  • 83