morilog / validator by morilog

Laravel Validation supports multiple scenarios
7,112
5
8
Package Data
Maintainer Username: morilog
Maintainer Contact: pcfeeler@gmail.com (bigsinoos)
Package Create Date: 2015-10-05
Package Last Update: 2019-09-25
Home Page:
Language: PHP
License: GPL
Last Refreshed: 2024-11-22 15:00:48
Package Statistics
Total Downloads: 7,112
Monthly Downloads: 109
Daily Downloads: 0
Total Stars: 5
Total Watchers: 8
Total Forks: 1
Total Open Issues: 0

Validator

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