PHP code example of projct1 / laravel-validation-base64

1. Go to this page and download the library: Download projct1/laravel-validation-base64 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/ */

    

projct1 / laravel-validation-base64 example snippets


use Projct1\LaravelValidationBase64\Rules\Base64Rule;

$request->validate([
    'signature' => ['

use Illuminate\Validation\Rule;
use Illuminate\Validation\Rules\File;
use Projct1\LaravelValidationBase64\Rules\FileBase64Rule;

$data = [
    'photos' => [
        [
            'name' => 'Фото зала',
            'src' => 'data:image/png;base64,iVBORw0...',
        ],
    ],
    'food' => [
        'tree' => [
            'items' => [
                [
                    'dish' => [
                        'id' => 1,
                        'name' => 'Стейк',
                    ],
                    'photo' => [
                        'src' => 'data:image/png;base64,iVBORw0...',
                    ],
                ],
            ],
        ],
    ],
];

// любые нативные файловые правила: https://laravel.com/docs/validation#validating-files
$fileRules = File::image()->dimensions(
    Rule::dimensions()->minWidth(1024)->minHeight(768)
);

$rules = [
    'photos.*.src' => new FileBase64Rule($fileRules, 'photos.*.name'),
    'food.tree.*.items.*.photo.src' => new FileBase64Rule($fileRules, 'food.tree.*.items.*.dish.name'),
];

$messages = [
    'dimensions' => ':Attribute имеет недостаточные размеры (:cur_width/:cur_height), необходимо не менее :min_width/:min_height пикселей',
];

$attributes = [
    'photos.*.src' => 'Фото галереи ":hint"',
    'food.tree.*.items.*.photo.src' => 'Фото блюда ":hint"',
];

$validator = validator($data, $rules, $messages, $attributes);
bash
php artisan vendor:publish --tag=base64-lang