mayoz / lumen-form-request
Adopt the Laravel Form Request to Lumen framework.
Installs: 2 236
Dependents: 0
Suggesters: 0
Security: 0
Stars: 2
Watchers: 2
Forks: 0
Open Issues: 0
Requires
- php: ^7.3|^8.0
- illuminate/auth: ^7.0|^8.0
- illuminate/console: ^7.0|^8.0
- illuminate/container: ^7.0|^8.0
- illuminate/contracts: ^7.0|^8.0
- illuminate/http: ^7.0|^8.0
- illuminate/support: ^7.0|^8.0
- illuminate/translation: ^7.0|^8.0
- illuminate/validation: ^7.0|^8.0
Requires (Dev)
- mockery/mockery: ^1.3.1
- phpunit/phpunit: ^8.4|^9.0
This package is auto-updated.
Last update: 2024-10-22 16:08:34 UTC
README
Adopt the Laravel Form Request to Lumen framework.
Installation
You can install the package via composer:
composer require mayoz/lumen-form-request
Register the service provider in your boostrap/app.php
configuration file:
$app->register(Illuminate\Foundation\Providers\FormRequestServiceProvider::class);
Usage
Let's continue the official Laravel's documantation.
Format Validation
If you want to format the verification messages, follow these steps:
Firstly import the ValidationException
class:
use Illuminate\Validation\ValidationException;
Add (if need) the dontReport
list:
/** * A list of the exception types that should not be reported. * * @var array */ protected $dontReport = [ // ... ValidationException::class, ];
And finally update the render
method:
/** * Render an exception into an HTTP response. * * @param \Illuminate\Http\Request $request * @param \Throwable $exception * @return \Symfony\Component\HttpFoundation\Response * * @throws \Throwable */ public function render($request, Throwable $exception) { if ($exception instanceof ValidationException) { return $this->convertValidationExceptionToResponse($exception, $request); } return parent::render($request, $exception); } /** * Create a response object from the given validation exception. * * @param \Illuminate\Validation\ValidationException $e * @param \Illuminate\Http\Request $request * @return \Symfony\Component\HttpFoundation\Response */ protected function convertValidationExceptionToResponse(ValidationException $e, $request) { if ($e->response) { return $e->response; } return response()->json([ 'message' => $e->getMessage(), 'errors' => $e->errors(), ], $e->status); }
License
This package is licensed under The MIT License (MIT).