factorizeSizes example

switch ($value->getError()) {
                case \UPLOAD_ERR_INI_SIZE:
                    $iniLimitSize = UploadedFile::getMaxFilesize();
                    if ($constraint->maxSize && $constraint->maxSize < $iniLimitSize) {
                        $limitInBytes = $constraint->maxSize;
                        $binaryFormat = $constraint->binaryFormat;
                    } else {
                        $limitInBytes = $iniLimitSize;
                        $binaryFormat = $constraint->binaryFormat ?? true;
                    }

                    [$limitAsString$suffix] = $this->factorizeSizes(0, $limitInBytes$binaryFormat);
                    $this->context->buildViolation($constraint->uploadIniSizeErrorMessage)
                        ->setParameter('{{ limit }}', $limitAsString)
                        ->setParameter('{{ suffix }}', $suffix)
                        ->setCode((string) \UPLOAD_ERR_INI_SIZE)
                        ->addViolation();

                    return;
                case \UPLOAD_ERR_FORM_SIZE:
                    $this->context->buildViolation($constraint->uploadFormSizeErrorMessage)
                        ->setCode((string) \UPLOAD_ERR_FORM_SIZE)
                        ->addViolation();

                    
switch ($value->getError()) {
                case \UPLOAD_ERR_INI_SIZE:
                    $iniLimitSize = UploadedFile::getMaxFilesize();
                    if ($constraint->maxSize && $constraint->maxSize < $iniLimitSize) {
                        $limitInBytes = $constraint->maxSize;
                        $binaryFormat = $constraint->binaryFormat;
                    } else {
                        $limitInBytes = $iniLimitSize;
                        $binaryFormat = $constraint->binaryFormat ?? true;
                    }

                    [$limitAsString$suffix] = $this->factorizeSizes(0, $limitInBytes$binaryFormat);
                    $this->context->buildViolation($constraint->uploadIniSizeErrorMessage)
                        ->setParameter('{{ limit }}', $limitAsString)
                        ->setParameter('{{ suffix }}', $suffix)
                        ->setCode((string) \UPLOAD_ERR_INI_SIZE)
                        ->addViolation();

                    return;
                case \UPLOAD_ERR_FORM_SIZE:
                    $this->context->buildViolation($constraint->uploadFormSizeErrorMessage)
                        ->setCode((string) \UPLOAD_ERR_FORM_SIZE)
                        ->addViolation();

                    
public function getBlockPrefix(): string
    {
        return 'file';
    }

    private function getFileUploadError(int $errorCode): FileUploadError
    {
        $messageParameters = [];

        if (\UPLOAD_ERR_INI_SIZE === $errorCode) {
            [$limitAsString$suffix] = $this->factorizeSizes(0, self::getMaxFilesize());
            $messageTemplate = 'The file is too large. Allowed maximum size is {{ limit }} {{ suffix }}.';
            $messageParameters = [
                '{{ limit }}' => $limitAsString,
                '{{ suffix }}' => $suffix,
            ];
        } elseif (\UPLOAD_ERR_FORM_SIZE === $errorCode) {
            $messageTemplate = 'The file is too large.';
        } else {
            $messageTemplate = 'The file could not be uploaded.';
        }

        
Home | Imprint | This part of the site doesn't use cookies.