3 namespace Drupal\file\Plugin\Validation\Constraint;
5 use Symfony\Component\Validator\Constraint;
6 use Symfony\Component\Validator\ConstraintValidator;
9 * Checks that a file referenced in a file field is valid.
11 class FileValidationConstraintValidator extends ConstraintValidator {
16 public function validate($value, Constraint $constraint) {
17 // Get the file to execute validators.
18 $file = $value->get('entity')->getTarget()->getValue();
19 // Get the validators.
20 $validators = $value->getUploadValidators();
21 // Checks that a file meets the criteria specified by the validators.
22 if ($errors = file_validate($file, $validators)) {
23 foreach ($errors as $error) {
24 $this->context->addViolation($error);