PHP code example of paliari / doctrine-validator

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

    

paliari / doctrine-validator example snippets



// Create your model extended to AbstractRansackModel.
class YourModel
{
    use \Paliari\Doctrine\TraitValidatorModel;

    //... fields ...

    /**
     * Override the method getEm is 
    }

}



class YourModel extends \Paliari\Doctrine\AbstractValidatorModel
{
    //... fields ...

    /**
     * Override the method getEm is function getEM()
    {
        // return EntityManager
    }
}


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

    protected static $validates_presence_of = [
        'email',
        'last_login' => ['on' => 'update'],
        'nike_name' => ['unless' => 'name']
    ];
    

    protected static $validates_length_of = [
        'name' => ['minimum' => 10, 'maximum' => 100],
        'nike_name' => ['in' => [3, 20]]
    ];
    

    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]
    ];
    

    protected static $validates_exclusion_of = [
        'field_name' => ['in' => ['a', 'b', 'c'], 'allow_blank' => true]
    ];
    

    protected static $validates_format_of = [
        'email' => ['with' => 'email'],
        'field_url' => ['with' => 'url'],
        'field_name' => ['without' => 'float']
    ];
    

    protected static $validates_numericality_of = [
        'ammount' => ['greater_than' => 5],
        'another_field' => ['only_integer' => true]
    ];
    

    protected static $validates_uniqueness_of = [
        'email',
        'number' => ['scope' => ['year']],
    ];
    

    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
    }
    

    protected static $before_validation = ['yourCallbackName'];

    public function yourCallbackName() { /* Do something here */}
    

    protected static $after_validation = ['anotherCallbackName'];
    

    protected static $before_validation_on_create = ['yourCallbackName'];
    

    protected static $after_validation_on_create = ['yourCallbackName'];
    

    protected static $before_validation_on_update = ['yourCallbackName'];
    

    protected static $after_validation_on_update = ['yourCallbackName'];
    

    protected static $before_validation_on_remove = ['yourCallbackName'];
    

    protected static $after_validation_on_remove = ['yourCallbackName'];
    

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');