An open API service indexing awesome lists of open source software.

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.

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);
}
```