2017 © Pedro Peláez
 

library validator

Form validator

image

enderlab/validator

Form validator

  • Wednesday, September 27, 2017
  • by ender9108
  • Repository
  • 0 Watchers
  • 0 Stars
  • 5 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 3 Versions
  • 0 % Grown

The README.md

Validator

Build Status Coverage Status, (*1)

Get started

<?php
$validator = new Validator([
    'email' => 'myemail@host.com',
    'slugProduct' => 'product-slug',
    'productName' => 'Toolbox'
]);

// add default validator
$validator->email('email')
          ->slug('slugProduct')
          ->length('firstname', 5, 64);

if (true == $validator->isValid()) {
    echo('Is valid !!!');
} else {
    print_r($validator->getErrors());
}

Default validator list

  • Boolean
$validator->boolean(string formFieldName);
  • Datetime
$validator->datetime(string formFieldName [, string dateFormat = Y-m-d H:i:s]);
  • Email
$validator->email(string formFieldName);
  • EqualTo
$validator->equalTo(string formFieldName, mixed compareValue);
  • GreaterThanOrEqual
$validator->greaterThanOrEqual(string formFieldName, mixed compareValue);
  • GreaterThan
$validator->greaterThan(string formFieldName, mixed compareValue);
  • Int
$validator->int(string formFieldName [,int min = null, int max = null]);
  • Ip
$validator->ip(string formFieldName [, bool isIpv6 = false]);
  • Length
$validator->length(string formFieldName [,int min = null, int max = null]);
  • LessThanOrEqual
$validator->lessThanOrEqual(string formFieldName, mixed compareValue);
  • LessThan
$validator->lessThan(string formFieldName, mixed compareValue);
  • NotEmpty
$validator->notEmpty(string formFieldName);
  • regex
$validator->regex(string formFieldName, string regex);
  • Slug
$validator->slug(string formFieldName);
  • Url
$validator->url(string formFieldName [, int flags = null]);

Add custom validator

<?php
use EnderLab\ValidatorInterface;

class MyCustomValidator implements ValidatorInterface
{
    /**
     * @var string
     */
    private $error = '';

    /**
     * @var mixed
     */
    private $value;

    /**
     * @var string
     */
    private $fieldName;

    /**
     * SlugValidator constructor.
     *
     * @param string   $fieldName
     * @param mixed    $value
     */
    public function __construct(string $fieldName, $value)
    {
        $this->value = $value;
        $this->fieldName = $fieldName;
    }

    /**
     * @return bool
     */
    public function isValid(): bool
    {
        if (!empty($this->value)) {
            return true;
        }

        $this->error = 'Field empty !!!';
        return true;
    }

    /**
     * @return string
     */
    public function getError(): string
    {
        return $this->error;
    }
}

$validator = new Validator([
    'email' => 'myemail@host.com',
    'slugProduct' => 'product-slug',
    'productName' => 'Toolbox'
]);

/**
 * $validator->addCustomValidator(
 *     Classname or instance implement ValidatorInterface,
 *     ...arguments
 * );
 */
$validator->addCustomValidator('MyCustomValidator');

if (true == $validator->isValid()) {
    echo('Is valid !!!');
} else {
    print_r($validator->getErrors());
}

The Versions

27/09 2017

dev-master

9999999-dev

Form validator

  Sources   Download

MIT

The Development Requires

by Alexandre Berthelot

27/09 2017
27/09 2017