PHP code example of paysera / lib-plugin-data-validation

1. Go to this page and download the library: Download paysera/lib-plugin-data-validation 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/ */

    

paysera / lib-plugin-data-validation example snippets


namespace '...';

use Paysera\DataValidator\Validator\AbstractValidator;

class SomeValidator extends AbstractValidator
{
    public function __construct(RepositoryInterface $repository /* dependencies here */)
    {
        parent::__construct();

        $rule = new EntityExists($repository);
        $this->addRule($rule);
        $this->setRuleMessage($rule->getName(), 'error message for entity-exists rule');
        
        // for customisation of error for specific fields
        // you can use kind of MessageRepository here as a dependency
        $this->setAttributeMessage('field_name', 'custom_error_this_field');
    }
}

$someValidator = new SomeValidator(/*dependencies here*/);

$rules = [
    'field1' => 'entity-exists|other_rules:with_parameters',
];

if (!$someValidator->validate($this->request->post /* or any other array with data */)) {
    $errors = $someValidator->getProcessedErrors();
}

/*
 * 'field' => [
 *     'entity-exists' => 'error message for entity-exists validator rule',
 *     'other_rules' => 'error message for other rules',
 * ]
 */

$this->setRuleMessage('entity-exists', 'Order status with ID=:id must exist');
// or
$this->setAttributeMessage('specific_field', 'Order status with ID=:id must exist');