class FileNameLengthConstraintValidator

Same name in other branches
  1. 11.x core/modules/file/src/Plugin/Validation/Constraint/FileNameLengthConstraintValidator.php \Drupal\file\Plugin\Validation\Constraint\FileNameLengthConstraintValidator

Validates the file name length constraint.

Hierarchy

Expanded class hierarchy of FileNameLengthConstraintValidator

File

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

Namespace

Drupal\file\Plugin\Validation\Constraint
View source
class FileNameLengthConstraintValidator extends BaseFileConstraintValidator {
    
    /**
     * {@inheritdoc}
     */
    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,
            ]);
        }
    }

}

Members

Title Sort descending Modifiers Object type Summary
BaseFileConstraintValidator::assertValueIsFile protected function Checks the value is of type FileInterface.
FileNameLengthConstraintValidator::validate public function

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