class FileValidator

Same name in other branches
  1. 10 core/modules/file/src/Validation/FileValidator.php \Drupal\file\Validation\FileValidator

Provides a class for file validation.

Hierarchy

  • class \Drupal\file\Validation\FileValidator implements \Drupal\file\Validation\FileValidatorInterface

Expanded class hierarchy of FileValidator

1 string reference to 'FileValidator'
file.services.yml in core/modules/file/file.services.yml
core/modules/file/file.services.yml
1 service uses FileValidator
file.validator in core/modules/file/file.services.yml
Drupal\file\Validation\FileValidator

File

core/modules/file/src/Validation/FileValidator.php, line 15

Namespace

Drupal\file\Validation
View source
class FileValidator implements FileValidatorInterface {
    
    /**
     * Creates a new FileValidator.
     *
     * @param \Symfony\Component\Validator\Validator\ValidatorInterface $validator
     *   The validator.
     * @param \Drupal\Core\Validation\ConstraintManager $constraintManager
     *   The constraint factory.
     * @param \Symfony\Contracts\EventDispatcher\EventDispatcherInterface $eventDispatcher
     *   The event dispatcher.
     * @param \Drupal\Core\Extension\ModuleHandlerInterface $moduleHandler
     *   The module handler.
     */
    public function __construct(ValidatorInterface $validator, ConstraintManager $constraintManager, EventDispatcherInterface $eventDispatcher, ModuleHandlerInterface $moduleHandler) {
    }
    
    /**
     * {@inheritdoc}
     */
    public function validate(FileInterface $file, array $validators) : ConstraintViolationListInterface {
        $constraints = [];
        foreach ($validators as $validator => $options) {
            // Create the constraint.
            // Options are an associative array of constraint properties and values.
            $constraints[] = $this->constraintManager
                ->create($validator, $options);
        }
        // Get the typed data.
        $fileTypedData = $file->getTypedData();
        $violations = $this->validator
            ->validate($fileTypedData, $constraints);
        $this->eventDispatcher
            ->dispatch(new FileValidationEvent($file, $violations));
        // Always check the insecure upload constraint.
        if (count($violations) === 0) {
            $insecureUploadConstraint = $this->constraintManager
                ->create('FileExtensionSecure', []);
            $violations = $this->validator
                ->validate($fileTypedData, $insecureUploadConstraint);
        }
        return $violations;
    }

}

Members

Title Sort descending Modifiers Object type Summary
FileValidator::validate public function
FileValidator::__construct public function Creates a new FileValidator.

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