function FileNameLengthConstraintValidator::validate

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

File

core/modules/file/src/Plugin/Validation/Constraint/FileNameLengthConstraintValidator.php, line 16

Class

FileNameLengthConstraintValidator
Validates the file name length constraint.

Namespace

Drupal\file\Plugin\Validation\Constraint

Code

public function validate(mixed $value, Constraint $constraint) {
  $file = $this->assertValueIsFile($value);
  if (!$constraint instanceof FileNameLengthConstraint) {
    throw new UnexpectedTypeException($constraint, FileNameLengthConstraint::class);
  }
  if (!$file->getFilename()) {
    $this->context
      ->addViolation($constraint->messageEmpty);
  }
  if (mb_strlen($file->getFilename()) > $constraint->maxLength) {
    $this->context
      ->addViolation($constraint->messageTooLong, [
      '%maxLength' => $constraint->maxLength,
    ]);
  }
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.