29.07.2016 Views

laravel-5

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Validation 378<br />

Customizing The Flashed Error Format<br />

If you wish to customize the format of the validation errors that are flashed to the session when<br />

validation fails, override the formatErrors on your base request (App\Http\Requests\Request).<br />

Don’t forget to import the Illuminate\Contracts\Validation\Validator class at the top of the<br />

file:<br />

.<br />

1 /**<br />

2 * {@inheritdoc}<br />

3 */<br />

4 protected function formatErrors(Validator $validator)<br />

5 {<br />

6 return $validator->errors()->all();<br />

7 }<br />

Working With Error Messages<br />

After calling the errors method on a Validator instance, you will receive an Illuminate\Support\MessageBag<br />

instance, which has a variety of convenient methods for working with error messages.<br />

Retrieving The First Error Message For A Field<br />

To retrieve the first error message for a given field, use the first method:<br />

.<br />

1 $messages = $validator->errors();<br />

2<br />

3 echo $messages->first('email');<br />

Retrieving All Error Messages For A Field<br />

If you wish to simply retrieve an array of all of the messages for a given field, use the get method:<br />

.<br />

1 foreach ($messages->get('email') as $message) {<br />

2 //<br />

3 }

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!