protected function validate(LifecycleEventArgs $args) {
        $entity = $args->getEntity();
        $metadata = $args->getEntityManager()->getClassMetadata(get_class($entity));
        if($this->shouldBeValidated($entity)) {
            $rules = $entity->getValidationRules();
            $fields = $metadata->getFieldNames();
            $data = [];
            foreach($fields as $field) {
                $data[$field] = $metadata->getFieldValue($entity, $field);
            }

            $this->validator->with($data, $rules);
            if(!$this->validator->passes()) {
                throw new InvalidEntityException($entity, $this->validator->errors());
            }
        }
    }
protected function validate(LifecycleEventArgs $args) {
        $entity = $args->getEntity();
        $metadata = $args->getEntityManager()->getClassMetadata(get_class($entity));
        if($this->shouldBeValidated($entity)) {
            $rules = $entity->getValidationRules();
            $fields = $metadata->getFieldNames();
            $data = [];
            foreach($fields as $field) {
                $data[$field] = $metadata->getFieldValue($entity, $field);
            }

            $this->validator->with($data, $rules);
            if(!$this->validator->passes()) {
                throw new InvalidEntityException($entity, $this->validator->errors());
            }
        }
    }
protected function validate ( LifecycleEventArgs $ args ) { $ entity = $ args -> getEntity ( ) ; $ metadata = $ args -> getEntityManager ( ) -> getClassMetadata ( get_class ( $ entity ) ) ; if ( $ this -> shouldBeValidated ( $ entity ) ) { $ rules = $ entity -> getValidationRules ( ) ; $ fields = $ metadata -> getFieldNames ( ) ; $ data = [ ] ; foreach ( $ fields as $ field ) { $ data [ $ field ] = $ metadata -> getFieldValue ( $ entity , $ field ) ; } $ this -> validator -> with ( $ data , $ rules ) ; if ( ! $ this -> validator -> passes ( ) ) { throw new InvalidEntityException ( $ entity , $ this -> validator -> errors ( ) ) ; } } }
Performs validation before the entity is persisted.

@param LifecycleEventArgs $args
@throws InvalidEntityException if the entity is invalid
@return boolean
Performs validation before the entity is persisted .