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 $target = $value->get('entity')->getTarget();
23 $file = $target->getValue();
24 // Get the validators.
25 $validators = $value->getUploadValidators();
26 // Checks that a file meets the criteria specified by the validators.
27 if ($errors = file_validate($file, $validators)) {
28 foreach ($errors as $error) {
29 $this->context->addViolation($error);