nimaebrazi / laravel-validator
Requires
- php: >=7.0
Requires (Dev)
- orchestra/testbench: ^3.8@dev
- phpunit/phpunit: ^7.5@dev
This package is not auto-updated.
Last update: 2023-12-07 17:14:49 UTC
README
Instalation
composer require nimaebrazi/laravel-validator
If using laravel 5.4.*
and older version you nedd add service provider in config/app.php
'providers' => [ ... \nimaebrazi\LaravelValidator\LaravelValidatorServiceProvider::class, ... ]
Publish config:
php artisan vendor:publish
You can change message path file in config: laravel_validator.php
Add this key in messages.php
file:
resources/lang/YOUR_LANGUAGE/messages.php
"validation_failed" => "messages.validation_failed"
This package throws an exception named ValidationException
. For handling Laravel Exception, add below code in Handler.php
file and custumize it for your project.
use nimaebrazi\LaravelValidator\src\Validator\ValidationException; use Symfony\Component\HttpFoundation\Response; ... /** * Render an exception into an HTTP response. * * @param \Illuminate\Http\Request $request * @param \Exception $exception * @return \Illuminate\Http\Response */ public function render($request, Exception $exception) { if($exception instanceof ValidationException){ return response()->json([ 'status' => Response::HTTP_UNPROCESSABLE_ENTITY, 'description' => $exception->getMessage(), 'data' => [ 'errors' => $exception->getErrors() ] ], 422); } return parent::render($request, $exception); }
Usage
Step 1:
Create a validation class:
\nimaebrazi\LaravelValidator\Validator\AbstractValidator
public function messages(): array
&
public function customAttributes(): array
methods are optional.
use nimaebrazi\LaravelValidator\Validator\AbstractValidator; class UpdateUserProfile extends AbstractValidator { /** * Rules of validation. * * @return array */ public function rules(): array { return [ 'name' => 'required|min:3', ]; } // OPTIONAL /** * Messages of rules. * * @return array */ public function messages(): array { return [ // ]; } // OPTIONAL /** * Custom attributes of rules. * * @return array */ public function customAttributes(): array { return [ // ]; } }
For more about:
Step 2: Inject class to a controller:
class ApiUserController extends Controller { /** * @param Request $request * @param UpdateUserProfile $validator * @throws \Exception * @throws \nimaebrazi\LaravelValidator\src\Validator\ValidationException */ public function update(Request $request, UpdateUserProfile $validator) { $validator->make($request->all())->validate(); }
Notes:
- When you call
validate
function package throws default exception. If you want handle other way:
class ApiUserController extends Controller { /** * @param Request $request * @param UpdateUserProfile $validator * @throws \Exception * @throws \nimaebrazi\LaravelValidator\src\Validator\ValidationException */ public function update(Request $request, UpdateUserProfile $validator) { $validator->make($request->all()); if($validator->fails()){ // your codes } if($validator->passes()){ // your codes } }
RuleManager Helper
Are you see document when use validator rules? I think it is so hard, when forget a rule and parameters. You can use RuleManager of this package.
use nimaebrazi\LaravelValidator\Validator\AbstractValidator; class UpdateUserProfile extends AbstractValidator { /** * Rules of validation. * * @return array */ public function rules(): array { return [ 'name' => $this->ruleManager()->required()->min('3')->make(), 'age' => $this->ruleManager()->required()->numeric()->max('3')->min('0')->make(), 'other' => $this->ruleManager()->required()->string()->make() ]; } }