PHP code example of ipedis / validation-handler

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

    

ipedis / validation-handler example snippets


use Ipedis\ValidationHandler\Data\DataWrapper;
use Ipedis\ValidationHandler\Data\Constraints\FileSize;
use Ipedis\ValidationHandler\Data\Constraints\MimeTypes;
use Ipedis\ValidationHandler\Validator\Result\ValidationResult;
use Ipedis\ValidationHandler\ConstraintFactory;

$file = __DIR__."/../tests/data/265kb.pdf";
$data = new DataWrapper(new SplFileInfo($file));

/**
 * build validator with list of constraints.
 */
$validator = ConstraintFactory::build(constraints: [
    new FileSize('100', 'k'),
    new MimeTypes(['application/pdf'])
]);
// run validations
$result = $validation->handle();

var_dump($result->isFailed(), $result->getErrorMessage());

/*
 * don't want to search for mimetype string? you can use built-in mimetypes helper 
 */
// example of mimetypes helper
$validator = ConstraintFactory::build(constraints: [
    new FileSize('1', 'M'),
    MimeTypes::with(PdfMimeType::class)
]);
/** @var ValidationResult $result */
$result = $validator->handle($data);

// should pass.
var_dump($result->isFailed(), $result->getErrorMessage());

/**
* If you do not concern about filesize but only want to check if file is image or pdf
 */
var_dump(ValidationHelper::isImage($data));
var_dump(ValidationHelper::isPdf($data));