PHP code example of koine / validator

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

    

koine / validator example snippets


class UserValidator extends Validator
{
    /**
     * {@inheritdocs}
     */
    protected function executeValidation($value)
    {
        if (!isset($value['name'])) {
            $this->getErrors()->add('name', 'you must set name');
        } elseif (!$value['name']) {
            $this->getErrors()->add('name', 'name cannot be empty');
        }

        if (!isset($value['lastName'])) {
            $this->getErrors()->add('lastName', 'you must set last name');
        } elseif (!$value['lastName']) {
            $this->getErrors()->add('lastName', 'last name cannot be empty');
        }
    }
}

$user = array(
    'name'     => 'Jon',
    'lastName' => '',
);

$validator = new UserValidator();
$validator->isValid($user); // false

$validator->getErrors()->toArray();
// array('lastName' => array('last name cannot be empty'))

$user['lastName'] = 'Doe';

$validator->isValid($user); // true

class UserValidator extends Validator
{
    /**
     * {@inheritdocs}
     */
    protected function executeValidation($value)
    {
        $this->validateUser($value);
    }

    private function validateUser(array $user)
    {
        if (!isset($user['name'])) {
            $this->getErrors()->add('name', 'you must set name');
        } elseif (!$user['name']) {
            $this->getErrors()->add('name', 'name cannot be empty');
        }

        if (!isset($user['lastName'])) {
            $this->getErrors()->add('lastName', 'you must set last name');
        } elseif (!$user['lastName']) {
            $this->getErrors()->add('lastName', 'last name cannot be empty');
        }
    }
}