Laravel Form Validation Primer
Forms, Laravel, PHP
Form validation logic can be held in an extension of the Request
class.
The Request class allows your script to get an instance of the current HTTP request. This is achieved by type hinting the Request class on the controller method. Dependency injection of The HTTP request then happens automatically.
Fortunately, Laravel has a built-in Artisan command that facilitates the generation of custom requests.
Create a Custom Request
Create a new form request class using Artisan:
This stubs out a form request class under /app/Http/Requests
- in this case the new file is: app/Http/Requests/ArticleRequest.php
.
The file contains a new ArticleRequest
class, which extends FormRequest
.
FormRequest
in turn extends the basic Laravel Request
class - which in turn extends the Symfony Request
component.
It’s probably better to keep the class name a bit general - ArticleRequest
is probably better than CreateArticleRequest
- as the same validation logic can be used for forms used to create and edit resources.
Set Up Validation Rules
The new form request class has an authorize()
and a rules()
method stubbed out. The authorize()
method can be used to check if the user is authorized to make the request - we might add a check for article ownership here.
The rules()
method allows you to set rules which must be passed. The controller form processing method will receive an instance of the request class - and will not progress if any rules fail.
Example Rules with Custom Error Messages
In the Controller
The new form request instance is passed into form processing methods on the Controller.
Dependency injection happens automatically by means of Laravel’s service container, but this can only take place if the request class is type-hinted on the controller method.
The store/update methods will not proceed until validation rules pass.
Example:
References
comments powered by Disqus