class FileValidationConstraintValidator

Same name in other branches
  1. 9 core/modules/file/src/Plugin/Validation/Constraint/FileValidationConstraintValidator.php \Drupal\file\Plugin\Validation\Constraint\FileValidationConstraintValidator
  2. 8.9.x core/modules/file/src/Plugin/Validation/Constraint/FileValidationConstraintValidator.php \Drupal\file\Plugin\Validation\Constraint\FileValidationConstraintValidator
  3. 10 core/modules/file/src/Plugin/Validation/Constraint/FileValidationConstraintValidator.php \Drupal\file\Plugin\Validation\Constraint\FileValidationConstraintValidator

Checks that a file referenced in a file field is valid.

Hierarchy

Expanded class hierarchy of FileValidationConstraintValidator

File

core/modules/file/src/Plugin/Validation/Constraint/FileValidationConstraintValidator.php, line 15

Namespace

Drupal\file\Plugin\Validation\Constraint
View source
class FileValidationConstraintValidator extends ConstraintValidator implements ContainerInjectionInterface {
    
    /**
     * Creates a new FileValidationConstraintValidator.
     *
     * @param \Drupal\file\Validation\FileValidatorInterface $fileValidator
     *   The file validator.
     */
    public function __construct(FileValidatorInterface $fileValidator) {
    }
    
    /**
     * {@inheritdoc}
     */
    public static function create(ContainerInterface $container) : self {
        return new static($container->get('file.validator'));
    }
    
    /**
     * {@inheritdoc}
     */
    public function validate($value, Constraint $constraint) : void {
        // Get the file to execute validators.
        $target = $value->get('entity')
            ->getTarget();
        if (!$target) {
            return;
        }
        $file = $target->getValue();
        // Get the validators.
        $validators = $value->getUploadValidators();
        // Always respect the configured maximum file size.
        $field_settings = $value->getFieldDefinition()
            ->getSettings();
        if (array_key_exists('max_filesize', $field_settings)) {
            $validators['FileSizeLimit'] = [
                'fileLimit' => Bytes::toNumber($field_settings['max_filesize']),
            ];
        }
        else {
            // Do not validate the file size if it is not set explicitly.
            unset($validators['FileSizeLimit']);
        }
        // Checks that a file meets the criteria specified by the validators.
        if ($violations = $this->fileValidator
            ->validate($file, $validators)) {
            foreach ($violations as $violation) {
                $this->context
                    ->addViolation($violation->getMessage());
            }
        }
    }

}

Members

Title Sort descending Modifiers Object type Summary Overriden Title
FileValidationConstraintValidator::create public static function Instantiates a new instance of this class. Overrides ContainerInjectionInterface::create
FileValidationConstraintValidator::validate public function
FileValidationConstraintValidator::__construct public function Creates a new FileValidationConstraintValidator.

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