A package that helps developer to segregate the validation logic from controller to a separate dedicated class. Lumen doesn't have any FormRequest
class like Laravel. This will let you do that.
- First of all, you will need composer installed. By running
composer require anik/form-request
from your terminal will install the package inside your project. - Register
Anik\Form\FormRequestServiceProvider
to yourbootstrap/app.php
as a provider
- Create a class that extends
Anik\Form\FormRequest
- Override
rule
method fromFormRequest
class. Define your validation rules inside that method. - You can define your messages by overriding
messages
method. authorize
method is also available to guard the request. Returntrue
orfalse
from this method. This will raiseUnauthorized
exception.- If the validation fails, it will throw exception of
Anik\Form\ValidationException
class. Handle it on yourapp/Exception/Handle.php
file.getResponse
method returns the messages. - Now you can use your Request class in method injections. All the methods from
Illuminate\Http\Request
class is available.