chhw / form-request
This package can use form request just as Laravel do.
1.0.4
2021-11-17 15:59 UTC
Requires
- php: >=7.2
- laravel/lumen-framework: >=7.0
README
This package can use form request just as Laravel do.
Installation
Install by composer
$ composer require chhw/form-request
In bootstrap/app.php
, you should:
- uncomment
$app->withFacades();
- 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 namespace App\Http\Requests; use CHHW\FormRequest\FormRequest; class CreatePost extends FormRequest { /** * Determine if the user is authorized to make this request. * * @return bool */ public function authorize() { return true; } /** * Get the validation rules that apply to the request. * * @return array */ public function rules() { return [ "title" => "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:
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
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 namespace App\Rules; use Illuminate\Contracts\Validation\Rule; class Uppercase implements Rule { /** * Determine if the validation rule passes. * * @param string $attribute * @param mixed $value * @return bool */ public function passes($attribute, $value) { return strtoupper($value) === $value; } /** * Get the validation error message. * * @return string */ public function message() { return 'The :attribute must be uppercase.'; } }
Get Request and Rule file by command:
php artisan make:request CreatePost php artisan make:rule Uppercase
Customize Localization Error Messages
just like laravel !
add resources/lang/en/validation.php
, and other country languages.
Error handler
app/Exceptions/Handler.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); }