2017 © Pedro Peláez
 

library validator

Universal and independent validator

image

ayeo/validator

Universal and independent validator

  • Wednesday, June 6, 2018
  • by ayeo
  • Repository
  • 7 Watchers
  • 4 Stars
  • 6,862 Installations
  • PHP
  • 1 Dependents
  • 0 Suggesters
  • 1 Forks
  • 0 Open issues
  • 12 Versions
  • 12 % Grown

The README.md

Validator

Independent library allows to simple validation other objects, (*1)

Install

Using composer, (*2)

composer require ayeo/validator

Example objects

Let's consider simplified objects as below, (*3)

class Company
{
    /** @var Address */
    private $address;

    /** @var string */ 
    public $name;

    /** @var Address */
    public function getAddress()
    {
        return $this->address();
    }
}
class Address
{
    /** @var string */ 
    public $street;

    /** @var string */ 
    public $town;

    /** @var string */ 
    public $countries;

}

Validation rules

To process validation we need to define our rules, (*4)

use Ayeo\Validator\ValidationRules

class CompanyValidationRules extends ValidationRules
{
    public function getRules()
    {
        return
        [
            ['company',
                [
                    ['name', new MinLength(5)],
                    ['address',
                        ['street', new MinLength(5)],
                        ['town', new MinLength(5)],
                        ['country', new OneOf(['USA', 'UK', 'Poland'])]
                    ]
                ] 
        ];            
    }
}

It is not too sophisticated but works just fine. As you can see we are able to validate nested objects. Validator is smart enough to get private and protected properties (if we got getter). Validator usage:, (*5)

$company = new Company;
$company->name = "Test Company";

$validator = new Validator(new CompanyValidationRules);
$isValid = $validator->validate($company);
$errors = $validator->getErrors();

Default values

Version 1.2 introduced default values support. In order to set default value you need to pass it as third argument. Default value will be used only if field value is null. Be aware that default value as still subject of further validation - if you set invalid default value it will result with error, (*6)

use Ayeo\Validator\ValidationRules

class CompanyValidationRules extends ValidationRules
{
    public function getRules()
    {
        return [['company', [['name', new MinLength(5), "Unknown name"]]];            
    }
}

Allow null

By default given validator will skip checking in case of null value. Of course you need some of them to check even null value. If constraint class implements CheckNull interface validator will force check field even if it is null. At the moment only NotNull constraint it one of this kind., (*7)

Availaible constraints

  • Length
  • MinLength
  • MaxLength
  • Integer
  • Numeric
  • NumericMin
  • NumericMax
  • NotNull
  • NonEmpty
  • ArrayOfType
  • IsArray
  • ClassInstance
  • NotClassInstance
  • LowerThanField
  • NoWhitespace
  • Email

Feel free to add some more!, (*8)

The Versions

06/06 2018

dev-master

9999999-dev https://github.com/ayeo/validator

Universal and independent validator

  Sources   Download

MIT

The Requires

  • php >=5.4.0

 

The Development Requires

validator

06/06 2018

1.2.4

1.2.4.0 https://github.com/ayeo/validator

Universal and independent validator

  Sources   Download

MIT

The Requires

  • php >=5.4.0

 

The Development Requires

validator

10/04 2018

dev-nested

dev-nested https://github.com/ayeo/validator

Universal and independent validator

  Sources   Download

MIT

The Requires

  • php >=5.4.0

 

The Development Requires

validator

10/04 2018

1.2.3

1.2.3.0 https://github.com/ayeo/validator

Universal and independent validator

  Sources   Download

MIT

The Requires

  • php >=5.4.0

 

The Development Requires

validator

22/02 2018

1.2.2

1.2.2.0 https://github.com/ayeo/validator

Universal and independent validator

  Sources   Download

MIT

The Requires

  • php >=5.4.0

 

The Development Requires

validator

06/11 2017

1.2.1

1.2.1.0 https://github.com/ayeo/validator

Universal and independent validator

  Sources   Download

MIT

The Requires

  • php >=5.4.0

 

The Development Requires

validator

23/12 2016

1.2.0

1.2.0.0 https://github.com/ayeo/validator

Universal and independent validator

  Sources   Download

MIT

The Requires

  • php >=5.4.0

 

The Development Requires

validator

14/12 2016

dev-new-constraints

dev-new-constraints https://github.com/ayeo/validator

Universal and independent validator

  Sources   Download

MIT

The Requires

  • php >=5.4.0

 

The Development Requires

validator

01/02 2016

1.1.0

1.1.0.0 https://github.com/ayeo/validator

Universal and independent validator

  Sources   Download

MIT

The Requires

  • php >=5.4.0

 

The Development Requires

validator

05/06 2015

1.0.1

1.0.1.0 https://github.com/ayeo/validator

Universal and independent validator

  Sources   Download

MIT

The Requires

  • php >=5.4.0

 

The Development Requires

validator

03/06 2015

dev-loco88-patch-1

dev-loco88-patch-1 https://github.com/ayeo/validator

Universal and independent validator

  Sources   Download

MIT

The Requires

  • php >=5.4.0

 

The Development Requires

validator

28/05 2015

1.0.0

1.0.0.0 https://github.com/ayeo/validator

Universal and independent validator

  Sources   Download

MIT

The Requires

  • php >=5.4.0

 

The Development Requires

validator