2017 © Pedro PelĂĄez
 

library doctrine-validator

Advanced model validator for Doctrine ORM (based on ActiveRecord)

image

paliari/doctrine-validator

Advanced model validator for Doctrine ORM (based on ActiveRecord)

  • Wednesday, December 20, 2017
  • by paliari
  • Repository
  • 2 Watchers
  • 1 Stars
  • 84 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 22 Versions
  • 0 % Grown

The README.md

doctrine-validator

Installation

composer require paliari/doctrine-validator

Configuration

Include the TraitValidatorModel in your model, (*1)


// Create your model extended to AbstractRansackModel. class YourModel { use \Paliari\Doctrine\TraitValidatorModel; //... fields ... /** * Override the method getEm is required. * You must return your EntityManager in this method * * @return \Doctrine\ORM\EntityManager */ public static function getEM() { // return EntityManager } }

Or you can make your model class extends from AbstractValidatorModel:, (*2)


class YourModel extends \Paliari\Doctrine\AbstractValidatorModel { //... fields ... /** * Override the method getEm is required. * You must return your EntityManager in this method * * @return \Doctrine\ORM\EntityManager */ public static function getEM() { // return EntityManager } }

Initialize validator

Add in your bootstrap:, (*3)

$eventManager = $entityManager->getEventManager();
$eventManager->addEventSubscriber(new \Paliari\Doctrine\ModelValidatorEventSubscriber());

Usage

Validators

Create a protected static property inside your model to validate. See the available options and examples:, (*4)

  • validates_presence_of, (*5)

    • Options: if, unless, on
    • Example:
    protected static $validates_presence_of = [
        'email',
        'last_login' => ['on' => 'update'],
        'nike_name' => ['unless' => 'name']
    ];
    
  • validates_size_of alias to validates_length_of, (*6)

    • Options: minimum, maximum, in|within
    • Example:
    protected static $validates_length_of = [
        'name' => ['minimum' => 10, 'maximum' => 100],
        'nike_name' => ['in' => [3, 20]]
    ];
    

    the maximum option is automatically setted by the field doc block, (*7)

  • validates_inclusion_of, (*8)

    • Options: in|within, allow_nil, allow_blank
    • Example:
    protected static $validates_inclusion_of = [
        'type' => ['in' => [1, 2, 3, 4]],
        'value' => ['in' => [1, 2, 3, 4], 'allow_nil' => true],
        'field_name' => ['in' => ['a', 'b', 'c'], 'allow_blank' => true]
    ];
    

    the boolean fields are automatically setted as true|false, (*9)

  • validates_exclusion_of, (*10)

    • Options: in|within, allow_nil, allow_blank
    • Example:
    protected static $validates_exclusion_of = [
        'field_name' => ['in' => ['a', 'b', 'c'], 'allow_blank' => true]
    ];
    
  • validates_format_of, (*11)

    • Options: with, without
    • Values: email, url, integer, boolean, ip, /[0-9a-z]/
    • Example:
    protected static $validates_format_of = [
        'email' => ['with' => 'email'],
        'field_url' => ['with' => 'url'],
        'field_name' => ['without' => 'float']
    ];
    
  • validates_numericality_of, (*12)

    • Options: greater_than, greater_than_or_equal_to, less_than, less_than_or_equal_to, equal_to, other_than, only_integer
    • Example:
    protected static $validates_numericality_of = [
        'ammount' => ['greater_than' => 5],
        'another_field' => ['only_integer' => true]
    ];
    
  • validates_uniqueness_of, (*13)

    • Options: scope
    • Example:
    protected static $validates_uniqueness_of = [
        'email',
        'number' => ['scope' => ['year']],
    ];
    
  • validates_custom, (*14)

    • Example:
    protected static $validates_custom = ['yourMethodName', 'otherYourMethodName'];
    
    public function yourMethodName() {
      if ($name == 'example') {
        $this->errors->add('name', '"name" cannot be "example"');
      }
    }
    
    public function otherYourMethodName() {
      // Do something here
    }
    

There is also a list of default options supported by every validator: 'if', 'unless', 'on', 'allow_nil' or 'allow_blank', (*15)

Callbacks

Create a protected static property with the callbacks inside your model. See the available options and examples:, (*16)

  • before_validation Execute before validation, (*17)

    • Example:
    protected static $before_validation = ['yourCallbackName'];
    
    public function yourCallbackName() { /* Do something here */}
    
  • after_validation Execute after validation, (*18)

    • Example:
    protected static $after_validation = ['anotherCallbackName'];
    
  • before_validation_on_create Execute before validation only create, (*19)

    • Example:
    protected static $before_validation_on_create = ['yourCallbackName'];
    
  • after_validation_on_create Execute after validation only create, (*20)

    • Example:
    protected static $after_validation_on_create = ['yourCallbackName'];
    
  • before_validation_on_update Execute before validation only update, (*21)

    • Example:
    protected static $before_validation_on_update = ['yourCallbackName'];
    
  • after_validation_on_update Execute after validation only update, (*22)

    • Example:
    protected static $after_validation_on_update = ['yourCallbackName'];
    
  • before_validation_on_remove Execute before validation only remove, (*23)

    • Example:
    protected static $before_validation_on_remove = ['yourCallbackName'];
    
  • after_validation_on_remove Execute after validation only remove, (*24)

    • Example:
    protected static $after_validation_on_remove = ['yourCallbackName'];
    

Custom Validators

Para adicionar validator customizado é só uar o método statico no model ModelName::addCustomValidator passando como argumento um callable que recebe como parametro o $model., (*25)

To add custom validator just use the static method in model ModelName :: addCustomValidator passing as argument a callable that takes $model as parameter., (*26)

Example:, (*27)

MyModel::addCustomValidator(function($model) {
  if ($model->isNewRecord()) {
      if (!MyValidator::instance()->isValid($model)) {
          foreach (MyValidator::instance()->getMessages() as $k => $item) {
              $model->errors->add($k, $item);
          }
      }
  }
});

// ou
MyModel::addCustomValidator('MyValidator::validate');

Development

Install dependencies

docker-compose -f docker-compose-cli.yml run --rm cli composer install

Testing

docker-compose -f docker-compose-cli.yml run --rm cli ./vendor/bin/phpunit

Publish a new version

# Generate a new tag
docker-compose -f docker-compose-cli.yml run --rm cli ./vendor/bin/bump -g --version patch|minor|major

git push && git push --tags

Authors

The Versions

20/12 2017

dev-master

9999999-dev https://github.com/paliari/doctrine-validator

Advanced model validator for Doctrine ORM (based on ActiveRecord)

  Sources   Download

MIT

The Requires

 

The Development Requires

20/12 2017

1.2.7

1.2.7.0 https://github.com/paliari/doctrine-validator

Advanced model validator for Doctrine ORM (based on ActiveRecord)

  Sources   Download

MIT

The Requires

 

The Development Requires

20/12 2017

1.2.6

1.2.6.0 https://github.com/paliari/doctrine-validator

Advanced model validator for Doctrine ORM (based on ActiveRecord)

  Sources   Download

MIT

The Requires

 

The Development Requires

13/12 2017

1.2.5

1.2.5.0 https://github.com/paliari/doctrine-validator

Advanced model validator for Doctrine ORM (based on ActiveRecord)

  Sources   Download

MIT

The Requires

 

The Development Requires

23/11 2017

1.2.4

1.2.4.0 https://github.com/paliari/doctrine-validator

Advanced model validator for Doctrine ORM (based on ActiveRecord)

  Sources   Download

MIT

The Requires

 

The Development Requires

08/08 2017

1.2.3

1.2.3.0 https://github.com/paliari/doctrine-validator

Advanced model validator for Doctrine ORM (based on ActiveRecord)

  Sources   Download

MIT

The Requires

 

The Development Requires

28/06 2017

1.2.2

1.2.2.0 https://github.com/paliari/doctrine-validator

Advanced model validator for Doctrine ORM (based on ActiveRecord)

  Sources   Download

MIT

The Requires

 

The Development Requires

14/06 2017

1.2.1

1.2.1.0 https://github.com/paliari/doctrine-validator

Advanced model validator for Doctrine ORM (based on ActiveRecord)

  Sources   Download

MIT

The Requires

 

The Development Requires

28/05 2017

1.2.0

1.2.0.0 https://github.com/paliari/doctrine-validator

Advanced model validator for Doctrine ORM (based on ActiveRecord)

  Sources   Download

MIT

The Requires

 

The Development Requires

13/05 2017

1.1.5

1.1.5.0 https://github.com/paliari/doctrine-validator

Advanced model validator for Doctrine ORM (based on ActiveRecord)

  Sources   Download

MIT

The Requires

 

The Development Requires

03/11 2016

1.1.4

1.1.4.0 https://github.com/paliari/doctrine-validator

Advanced model validator for Doctrine ORM (based on ActiveRecord)

  Sources   Download

MIT

The Requires

 

The Development Requires

03/11 2016

1.1.3

1.1.3.0

Advanced model validator for Doctrine ORM (based on ActiveRecord)

  Sources   Download

MIT

The Requires

 

The Development Requires

17/10 2016

1.1.2

1.1.2.0

Advanced model validator for Doctrine ORM (based on ActiveRecord)

  Sources   Download

MIT

The Requires

 

The Development Requires

14/10 2016

1.1.1

1.1.1.0

Advanced model validator for Doctrine ORM (based on ActiveRecord)

  Sources   Download

MIT

The Requires

 

The Development Requires

13/10 2016

1.1.0

1.1.0.0

Advanced model validator for Doctrine ORM (based on ActiveRecord)

  Sources   Download

MIT

The Requires

 

The Development Requires

12/10 2016

1.0.6

1.0.6.0

Advanced model validator for Doctrine ORM (based on ActiveRecord)

  Sources   Download

MIT

The Requires

 

The Development Requires

11/10 2016

1.0.5

1.0.5.0

Advanced model validator for Doctrine ORM (based on ActiveRecord)

  Sources   Download

MIT

The Requires

 

The Development Requires

11/10 2016

1.0.4

1.0.4.0

Advanced model validator for Doctrine ORM (based on ActiveRecord)

  Sources   Download

MIT

The Requires

 

The Development Requires

11/10 2016

1.0.3

1.0.3.0

Advanced model validator for Doctrine ORM (based on ActiveRecord)

  Sources   Download

MIT

The Requires

 

The Development Requires

10/10 2016

1.0.2

1.0.2.0

Advanced model validator for Doctrine ORM (based on ActiveRecord)

  Sources   Download

MIT

The Requires

 

The Development Requires

07/10 2016

1.0.1

1.0.1.0

Advanced model validator for Doctrine ORM (based on ActiveRecord)

  Sources   Download

MIT

The Requires

 

The Development Requires

06/10 2016

0.0.1

0.0.1.0

Advanced model validator for Doctrine ORM (based on ActiveRecord)

  Sources   Download

MIT

The Requires