You are here

public function FileValidationConstraintValidator::validate in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 core/modules/file/src/Plugin/Validation/Constraint/FileValidationConstraintValidator.php \Drupal\file\Plugin\Validation\Constraint\FileValidationConstraintValidator::validate()

Checks if the passed value is valid.

Parameters

mixed $value The value that should be validated:

Constraint $constraint The constraint for the validation:

Overrides ConstraintValidatorInterface::validate

File

core/modules/file/src/Plugin/Validation/Constraint/FileValidationConstraintValidator.php, line 21
Contains \Drupal\file\Plugin\Validation\Constraint\FileValidationConstraintValidator.

Class

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

Namespace

Drupal\file\Plugin\Validation\Constraint

Code

public function validate($value, Constraint $constraint) {

  // Get the file to execute validators.
  $file = $value
    ->get('entity')
    ->getTarget()
    ->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);
    }
  }
}