filterTools = $filterTools; } /** * Checks if the passed value is valid. * * @param mixed $value The value that should be validated * @param Constraint $constraint The constraint for the validation */ public function validate($value, Constraint $constraint) { if (!$constraint instanceof AllowedFileExtension) { throw new UnexpectedTypeException($constraint, AllowedFileExtension::class); } if ($value instanceof UploadedFile) { if ($this->context->getObject() instanceof Attachment) { /** @var Attachment $attachment */ $attachment = $this->context->getObject(); } elseif ($this->context->getObject() instanceof FormInterface) { $attachment = $this->context->getObject()->getParent()->getData(); } else { return; } $attachment_type = $attachment->getAttachmentType(); //Only validate if the attachment type has specified an filetype filter: if ($attachment_type === null || empty($attachment_type->getFiletypeFilter())) { return; } if (!$this->filterTools->isExtensionAllowed( $attachment_type->getFiletypeFilter(), $value->getClientOriginalExtension() )) { $this->context->buildViolation('validator.file_ext_not_allowed')->addViolation(); } } } }