geoffs3310 geoffs3310 - 1 month ago 4x
PHP Question

Laravel 5 request - How to show individual validation errors

I have created a request in L5 to handle saving a contact which looks like so:

<?php namespace App\Http\Requests;

use App\Http\Requests\Request;

class AddContactRequest extends Request {

* 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 [
'category_ids' => 'required|array|exists:categories,id',
'gender' => 'required|in:Male,Female',
'first_name' => 'required',
'last_name' => 'required',
'company' => 'required',
'position' => 'required',
'website_url' => 'url',
'facebook_url' => 'url',
'twitter_url' => 'url',
'instagram_url' => 'url',
'misc_url1' => 'url',
'misc_url2' => 'url',
'address_1' => 'required',
'citytown' => 'required',
'postcode' => 'required',
'country_id' => 'required|integer|exists:countries,id',
'work_email' => 'email',
'home_email' => 'email',
'work_phone' => '',
'mobile_phone' => '',
'home_phone' => ''

* Error messages for validation rules
* @return array
public function messages()
return [
'category_ids.required' => 'You must select at least one category.',
'category_ids.array' => 'Categories must be an array.',
'category_ids.exists' => 'The category does not exist.',
'gender.required' => 'The gender field is required.',
'' => 'The gender is invalid.',
'first_name.required' => 'The first name field is required.',
'last_name.required' => 'The last name field is required.',
'company.required' => 'The company field is required.',
'position.required' => 'The position field is required.',
'website_url.url' => 'The website URL is not a valid URL.',
'facebook_url.url' => 'The facebook URL is not a valid URL.',
'twitter_url.url' => 'The twitter URL is not a valid URL.',
'instagram_url.url' => 'The instagram URL is not a valid URL.',
'misc_url1.url' => 'The miscellaneous URL 1 field is not a valid URL',
'misc_url2.url' => 'The miscellaneous URL 2 field is not a valid URL',
'address_1.required' => 'The address 1 field is required.',
'citytown.required' => 'The city / town field is required.',
'postcode.required' => 'The postcode field is required.',
'country_id.required' => 'The country field is required.',
'country_id.integer' => 'The country field must contain an integer.',
'country_id.exists' => 'The country is invalid.',
'' => 'The work email field is not a valid email address.',
'' => 'The home email field is not a valid email address.'


When the validation fails I'm able to output all of the errors in my view with the following code:

@if (count($errors) > 0)
<div class="alert alert-danger">
<strong>Whoops!</strong> There were some problems with your input.<br><br>
@foreach ($errors->all() as $error)
<li>{{ $error }}</li>

But I want to show individual errors and add a class to the form group if that field has an error like so:

<div class="form-group <?php echo ($messages->has('first_name')) ? 'has-error' : ''; ?>">
<label for="first_name" class="control-label col-xs-12 col-sm-3 col-md-4 col-lg-4">First Name</label>
<div class="col-xs-12 col-sm-6 col-md-8 col-lg-8">
{!! Form::text('first_name', '', array('class' => 'form-control')) !!}

The $messages->has() method doesn't seem to work though I get the following error:

Whoops, looks like something went wrong.

ErrorException in 6351f2a902813f4fd7aa70b7ef38354d line 34:
Undefined variable: messages (View: C:\xampp\htdocs\fourteen-ten\resources\views\admin\contacts\add.blade.php)

Does anyone know why I am getting this error?


OK thanks to Sulthan I was able to figure it out. Instead of:


I should use: