FileValidationConstraintValidator.php

Same filename in other branches
  1. 9 core/modules/file/src/Plugin/Validation/Constraint/FileValidationConstraintValidator.php
  2. 10 core/modules/file/src/Plugin/Validation/Constraint/FileValidationConstraintValidator.php
  3. 11.x core/modules/file/src/Plugin/Validation/Constraint/FileValidationConstraintValidator.php

Namespace

Drupal\file\Plugin\Validation\Constraint

File

core/modules/file/src/Plugin/Validation/Constraint/FileValidationConstraintValidator.php

View source
<?php

namespace Drupal\file\Plugin\Validation\Constraint;

use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\ConstraintValidator;

/**
 * Checks that a file referenced in a file field is valid.
 */
class FileValidationConstraintValidator extends ConstraintValidator {
    
    /**
     * {@inheritdoc}
     */
    public function validate($value, Constraint $constraint) {
        // Get the file to execute validators.
        $target = $value->get('entity')
            ->getTarget();
        if (!$target) {
            return;
        }
        $file = $target->getValue();
        // Get the validators.
        $validators = $value->getUploadValidators();
        // Checks that a file meets the criteria specified by the validators.
        if ($errors = file_validate($file, $validators)) {
            foreach ($errors as $error) {
                $this->context
                    ->addViolation($error);
            }
        }
    }

}

Classes

Title Deprecated Summary
FileValidationConstraintValidator Checks that a file referenced in a file field is valid.

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.