jedrzej/validator-extended-syntax

Laravel 4/5 package that adds validator negation to Laravel's validation module
11,501 3
Install
composer require jedrzej/validator-extended-syntax
Latest Version:0.0.4
License:MIT
Last Updated:Oct 25, 2018
Links: GitHub  ·  Packagist
Maintainer: jedrzej

Extended Validation for Laravel 5

This package extends Laravel's validation syntax with the following:

  • aliasing validation rule configurations
  • negating validation rules
  • using automatically replaced placeholders

Composer install

Add the following line to composer.json file in your project:

"jedrzej/validator-extended-syntax": "0.0.3"

or run the following in the commandline in your project's root folder:

composer require "jedrzej/validator-extended-syntax" "0.0.3"

Usage

In order to extend validator syntax, you need to register ValidationServiceProvider in your config/app.php:

    'providers' => [
        ...
        /*
         * Custom proviers
         */
        'Jedrzej\Validation\ValidationServiceProvider'
    ];

Aliasing validation rules

It is possible to alias often used rule configuration to allow for reuse. This is an alternative to writing custom validation rules.

    // validate if string is a hex calue
    Validator::alias('hex', 'regex:/^[0-9a-fA-F]+$/');
    $rules = [
      'value' => 'hex'
    ];
    $validator = Validator::make($data, $rules);

    // passing arguments to aliases
    // validate number is a positive integer no larger than 100
    Validator::alias('positive_limited', 'between:1,?');
    $rules = [
      'value' => 'positive_limited:100'
    ];
    $validator = Validator::make($data, $rules);

    // record exists with is_active flag set
    Validator::alias('active_exists', 'exists:?,?,is_active,1');
    $rules = [
      'user_id' => 'active_exists:users,id'
    ];
    $validator = Validator::make($data, $rules);

Negating validation results

When defining validation rules, you can negate chosen rule by prepending its name with exclamation mark. Negated validation rules will fail when not negated rule would pass and vice versa.

    $rules = ['string' => 'min:3']; //validate if string is at least 3 characters long
    $data = ['string' => 'abcde'];
    $result = Validator::make($data, $rules)->passes(); // TRUE

    $rules = ['string' => 'min:3'];
    $data = ['string' => 'ab'];
    $result = Validator::make($data, $rules)->passes(); // FALSE

    $rules = ['string' => '!min:3'];
    $data = ['string' => 'abcde'];
    $result = Validator::make($data, $rules)->passes(); // FALSE

    $rules = ['string' => '!min:3'];
    $data = ['string' => 'ab'];
    $result = Validator::make($data, $rules)->passes(); // TRUE

Placeholders in validation rules

If validation of one field needs to use the value of another field as parameter, you can use a {{parameter_name}} placeholder in rule definition instead of parameter value. Value of corresponding field will be passed to validator instead of the placeholder. If the corresponding value is missing in validated data set, the value will be taken from Config. If it's missing in config, NULL will be used.

    $rules = [
        'user_id' => 'exists:users,id'
        'email'   => 'unique:users,email,{{user_id}}',
        'age'     => 'min:{{app.min_age}}
    ];

Related Packages

propaganistas/laravel-phone

Adds phone number functionality to Laravel based on Google's libphonenumber API.

41,379,100 3,024
prettus/laravel-validation

Laravel Validation Service

11,605,477 406
pixelpeter/laravel5-isocodes-validation

Laravel 5 wrapper for the IsoCodes Validation library from ronanguilloux

87,789 19
skysplit/laravel5-intl-translation

Laravel 5 package for better translation syntax using php-intl extension

106,714 10
proengsoft/laravel-jsvalidation

Validate forms transparently with Javascript reusing your Laravel Validation Rul...

2,389,968 1,141