laratalks / validator
Laravel Validation supports multiple scenarios
Installs: 7 119
Dependents: 0
Suggesters: 0
Security: 0
Stars: 5
Watchers: 8
Forks: 1
Open Issues: 0
Requires
- illuminate/contracts: ^5.4 || ^6.0
- illuminate/validation: ^5.4 || ^6.0
This package is auto-updated.
Last update: 2024-11-25 23:47:55 UTC
README
Simple validator library for Laravel framework with multiple scenarios. By using this package, you write your validator once and use every where and moderate your Domain rules easily.
Installation
Use composer:
composer require laratalks/validator
Usage
Your valdiation classes must extends Laratalks\Valdiator\AbstarctValdiator
:
<?php #UserValidator.php namespace YourApp\Validators; use Laratalks\Validator\AbstractValidator; class UserValidator extends AbstractValidator { protected $registrationRules = [ 'name' => ['required'], 'email' => ['required', 'email'], 'home_page' => ['required', 'url'] ]; protected $activationRules = [ 'id' => ['required', 'exists:users'], 'token' => ['required', 'min:64'] ]; protected $anotherScenarioRules = [ 'key1' => ['rule1', 'rule2'], 'key2' => ['rule1', 'rule2'] ]; }
You must inject validatio in your methods or controller __construct
method to using it:
<?php # UserController.php namespace Laratalks\Validator; use YourApp\Validators\UserValidator; use Laratalks\Validator\Exceptions\ValidationException; class UserController extends Controller { public function register(Request $request, UserValidator $valdiator) { try { // validate user input $valdiator ->setScenario('registration') ->validate($request->all()); } catch (ValidationException $e) { // catch errors return $e->getErrors(); } } }