PHP code example of jedrzej / validator-extended-syntax

1. Go to this page and download the library: Download jedrzej/validator-extended-syntax library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

jedrzej / validator-extended-syntax example snippets


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

    // 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);

    $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

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