a6da920c669762428b29ae828301d320e7f153c3
[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     $target = $value->get('entity')->getTarget();
19     if (!$target) {
20       return;
21     }
22
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);
30       }
31     }
32   }
33
34 }