972aa69767ddc23fb2c757105e344432bea45166
[yaffs-website] / web / core / modules / file / src / Plugin / Validation / Constraint / FileValidationConstraintValidator.php
1 <?php
2
3 namespace Drupal\file\Plugin\Validation\Constraint;
4
5 use Symfony\Component\Validator\Constraint;
6 use Symfony\Component\Validator\ConstraintValidator;
7
8 /**
9  * Checks that a file referenced in a file field is valid.
10  */
11 class FileValidationConstraintValidator extends ConstraintValidator {
12
13   /**
14    * {@inheritdoc}
15    */
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);
25       }
26     }
27   }
28
29 }