06.10.2016 Views

laravel-5

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Validation 492<br />

Named Error Bags<br />

If you have multiple forms on a single page, you may wish to name the MessageBag of errors,<br />

allowing you to retrieve the error messages for a specific form. Simply pass a name as the second<br />

argument to withErrors:<br />

1 return redirect('register')<br />

2 ->withErrors($validator, 'login');<br />

You may then access the named MessageBag instance from the $errors variable:<br />

1 {{ $errors->login->first('email') }}<br />

After Validation Hook<br />

The validator also allows you to attach callbacks to be run after validation is completed. This allows<br />

you to easily perform further validation and even add more error messages to the message collection.<br />

To get started, use the after method on a validator instance:<br />

1 $validator = Validator::make(...);<br />

2<br />

3 $validator->after(function($validator) {<br />

4 if ($this->somethingElseIsInvalid()) {<br />

5 $validator->errors()->add('field', 'Something is wrong with this field!'\<br />

6 );<br />

7 }<br />

8 });<br />

9<br />

10 if ($validator->fails()) {<br />

11 //<br />

12 }<br />

Form Request Validation<br />

For more complex validation scenarios, you may wish to create a “form request”. Form requests<br />

are custom request classes that contain validation logic. To create a form request class, use the<br />

make:request Artisan CLI command:

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

Saved successfully!

Ooh no, something went wrong!