https://github.com/wangchristine/form-request
This package can use form request just as Laravel do.
https://github.com/wangchristine/form-request
form-request lumen validation
Last synced: 29 days ago
JSON representation
This package can use form request just as Laravel do.
- Host: GitHub
- URL: https://github.com/wangchristine/form-request
- Owner: wangchristine
- Created: 2021-10-30T06:42:25.000Z (over 4 years ago)
- Default Branch: master
- Last Pushed: 2021-11-17T16:01:39.000Z (over 4 years ago)
- Last Synced: 2025-05-23T06:10:18.453Z (about 1 year ago)
- Topics: form-request, lumen, validation
- Language: PHP
- Homepage:
- Size: 10.7 KB
- Stars: 2
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Lumen Form Request
> This package can use form request just as Laravel do.
# Installation
Install by composer
```bash
$ composer require chhw/form-request
```
In `bootstrap/app.php`, you should:
1. uncomment `$app->withFacades();`
2. add `$app->register(CHHW\FormRequest\FormRequestServiceProvider::class);`
# Usage
> Just like the way to use Laravel !
### By structured file:
add something request like: `app/Http/Requests/CreatePost.php`,extends `CHHW\FormRequest\FormRequest`
> app/Http/Requests/CreatePost.php
```php
"required|string",
];
}
public function withValidator($validator)
{
$validator->after(function ($validator) {
if (true) {
$validator->errors()->add('field', 'Something is wrong with this field!');
}
});
}
}
```
> app/Http/Controllers/ExampleController.php:
```php
use App\Http\Requests\CreatePost;
class ExampleController extends Controller
{
public function test(CreatePost $request)
{
dd($request->all());
// ...
}
}
```
### Or in controller:
> app/Http/Controllers/ExampleController.php
```php
use App\Rules\Uppercase;
use Illuminate\Http\Request;
class ExampleController extends Controller
{
public function create(Request $request)
{
$request->validate(["author" => ["required", new Uppercase],]);
// ...
}
}
```
> app/Rules/Uppercase.php
```php
just like laravel !
add `resources/lang/en/validation.php`, and other country languages.
# Error handler
> app/Exceptions/Handler.php
```php
public function render($request, Throwable $exception)
{
if($exception instanceof ValidationException){
return response()->json(["message" => $exception->getMessage(), "details" => $exception->errors()]);
}
return parent::render($request, $exception);
}
```