https://github.com/ssi-anik/form-request
Form request is a package for Lumen that lets developer validate form requests like Laravel does.
https://github.com/ssi-anik/form-request
form-request-validation form-validation lumen lumen-package php validation
Last synced: 2 months ago
JSON representation
Form request is a package for Lumen that lets developer validate form requests like Laravel does.
- Host: GitHub
- URL: https://github.com/ssi-anik/form-request
- Owner: ssi-anik
- License: mit
- Created: 2017-08-25T16:41:38.000Z (almost 8 years ago)
- Default Branch: master
- Last Pushed: 2024-03-02T10:50:52.000Z (over 1 year ago)
- Last Synced: 2025-04-02T02:36:31.332Z (3 months ago)
- Topics: form-request-validation, form-validation, lumen, lumen-package, php, validation
- Language: PHP
- Homepage: https://packagist.org/packages/anik/form-request
- Size: 24.4 KB
- Stars: 50
- Watchers: 2
- Forks: 26
- Open Issues: 1
-
Metadata Files:
- Readme: readme.md
- License: LICENSE
Awesome Lists containing this project
README
Form-Request
[](https://codecov.io/gh/ssi-anik/form-request)
[](//packagist.org/packages/anik/form-request)
[](//packagist.org/packages/anik/form-request)
===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.### Installation
1. Install the package by running `composer require anik/form-request` from your terminal being in the project
directory.
2. Register `\Anik\Form\FormRequestServiceProvider::class` to your `bootstrap/app.php` as a provider.```php
// bootstrap/app.php
$app->register(\Anik\Form\FormRequestServiceProvider::class);
```### How to use?
- Create a class that extends `Anik\Form\FormRequest` class.
- You must override `rules` method of the `Anik\Form\FormRequest` class. Define your validation rules in it. Must return
an **array**.
- You can define validation **messages** by overriding `messages` method. Default is `[]`.
- You can define custom pretty **attribute** names by overriding `attributes` method. Default is `[]`.
- You can override `authorize` method to define the authorization logic if the client is authorized to submit the form.
Must return a boolean value. Default is `true`. When returning `false`, it'll
raise `\Illuminate\Auth\Access\AuthorizationException` exception.
- If the validation fails, it will throw `Illuminate\Validation\ValidationException`.
- By default, it returns response in `{"message": "The given data was invalid.", "errors": []}` format with status
code `422`. Handle the exception in `app/Exceptions/Handler.php`'s `render` method if you want to modify the
response.
- Override the `statusCode` method to return the status of your choice. Must return `int`. Default is `422`.
- Override the `errorMessage` method to return the message of your choice. Must return `string`. Default
is `The given data was invalid.`
- Override the `errorResponse` method to return response of your choice when the validation fails. Must return
either of type `\Symfony\Component\HttpFoundation\Response` or `null`.
- Now you can inject your **Request** class through the method injections. All the methods
of `Laravel\Lumen\Http\Request` class is available in your request class.
- The `FormRequest::validated()` method will return the validated data when the validation passes.