zardon
zardon

Reputation: 1651

Laravel5 - Request validation always passes

I am learning/using Laravel5 and using the Request generator tool to make a custom request validation handler;

php artisan make:request <nameOfRequestFile>

And I find that my validation always passes, but I don't know why.

On my view I do a vardump of the errors;

{{ var_dump($errors) }}

And when I submit an empty form it always inserts a new record, when it should fail.

The var dump reports;

object(Illuminate\Support\ViewErrorBag)#135 (1) { ["bags":protected]=> array(0) { } }

Which seems to suggest the error bag is always empty.

My request file is pretty simple

namespace App\Http\Requests;
use App\Http\Requests\Request;

class PublishDriverRequest extends Request
{
    public function authorize()
    {
        return true;
    }

    public function rules()
    {
        return [
          'name.required|min:3' => 'Please provide a name.',
          'companyName.required|min:3' => 'Please provide a company name.'
        ];
    }
}

I checked my input form and the names are the same;

<input type="text" name="name" id="name" class="form-control">
<input type="text" name="companyName" id="companyName" class="form-control">

And I checked my database, the names are the same too.

My controller when storing the data is equally straight forward;

  public function store(PublishDriverRequest $request) {
        Driver::create($request->all());
        return redirect('/');
    }

So I'm not sure why the validation always passes; if I submit an empty form it should fail as the rules indicate minimum length and required.

What am I doing wrong?

Upvotes: 2

Views: 149

Answers (1)

smartrahat
smartrahat

Reputation: 5649

Change you validation rules to:

public function rules()
{
    return [
      'name' => 'required|min:3',
      'companyName' => 'required|min:3',
    ];
}

To show custom validation error:

public function messages()
{
    return [
        'name.required' => 'Please provide a name.',
        'companyName.required' => 'Please provide a company name.',
    ];
}

Note: Use message() method before rules()

Upvotes: 2

Related Questions