php - Laravel 5 request - How to show individual validation errors -



php - Laravel 5 request - How to show individual validation errors -

i have created request in l5 handle saving contact looks so:

<?php namespace app\http\requests; utilize app\http\requests\request; class addcontactrequest extends request { /** * determine if user authorized create request. * * @return bool */ public function authorize() { homecoming true; } /** * validation rules apply request. * * @return array */ public function rules() { homecoming [ '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 validation rules * @return array */ public function messages() { homecoming [ 'category_ids.required' => 'you must select @ to the lowest degree 1 category.', 'category_ids.array' => 'categories must array.', 'category_ids.exists' => 'the category not exist.', 'gender.required' => 'the gender field required.', 'gender.in' => 'the gender invalid.', 'first_name.required' => 'the first name field required.', 'last_name.required' => 'the lastly name field required.', 'company.required' => 'the company field required.', 'position.required' => 'the position field required.', 'website_url.url' => 'the website url not valid url.', 'facebook_url.url' => 'the facebook url not valid url.', 'twitter_url.url' => 'the twitter url not valid url.', 'instagram_url.url' => 'the instagram url not valid url.', 'misc_url1.url' => 'the miscellaneous url 1 field not valid url', 'misc_url2.url' => 'the miscellaneous url 2 field not valid url', 'address_1.required' => 'the address 1 field required.', 'citytown.required' => 'the city / town field required.', 'postcode.required' => 'the postcode field required.', 'country_id.required' => 'the country field required.', 'country_id.integer' => 'the country field must contain integer.', 'country_id.exists' => 'the country invalid.', 'work_email.email' => 'the work email field not valid email address.', 'home_email.email' => 'the home email field not valid email address.' ]; } }

when validation fails i'm able output of errors in view next code:

@if (count($errors) > 0) <div class="alert alert-danger"> <strong>whoops!</strong> there problems input.<br><br> <ul> @foreach ($errors->all() $error) <li>{{ $error }}</li> @endforeach </ul> </div> @endif

but want show individual errors , add together class form grouping if field has error 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')) !!} </div> </div>

the $messages->has() method doesn't seem work though next error:

whoops, looks went wrong. 2/2 errorexception in 6351f2a902813f4fd7aa70b7ef38354d line 34: undefined variable: messages (view: c:\xampp\htdocs\fourteen-ten\resources\views\admin\contacts\add.blade.php)

does know why getting error?

to display error individually. shall place under each element

<div class="error">{{ $errors->first('category_ids') }}</div>

note :

you shall create class named error in css this.

this display errors if occcurs

php laravel laravel-5

Comments

Popular posts from this blog

java - How to set log4j.defaultInitOverride property to false in jboss server 6 -

c - GStreamer 1.0 1.4.5 RTSP Example Server sends 503 Service unavailable -

Using ajax with sonata admin list view pagination -