{"record":{"id":"4274b7e05ca7b19e","repo":"flarum/framework","slug":"validation-error-translated-message-for-filename","errorCode":null,"errorMessage":"validation.<error> (translated message for <filename>)","messagePattern":"validation\\.<error> \\(translated message for <filename>\\)","errorType":"validation","errorClass":"ValidationException","httpStatus":null,"severity":"warning","filePath":"framework/core/src/Foundation/AbstractImageValidator.php","lineNumber":135,"sourceCode":"        if ($size === null || $size / 1024 > $maxSize) {\n            $this->raise('max.file', [':max' => $maxSize], 'max');\n        }\n    }\n\n    protected function raise(string $error, array $parameters = [], ?string $rule = null): void\n    {\n        // When we switched to intl ICU message format, the translation parameters\n        // have become required to be in the format `{param}`.\n        // Therefore, we cannot use the translator to replace the string params.\n        // We use the laravel validator to make the replacements instead.\n        $message = $this->laravelValidator->makeReplacements(\n            $this->translator->trans(\"validation.$error\"),\n            $this->filename,\n            $rule ?? $error,\n            array_values($parameters)\n        );\n\n        throw new ValidationException([$this->filename => $message]);\n    }\n\n    public function getMaxSize(): int\n    {\n        return 2048;\n    }\n\n    /**\n     * The maximum number of pixels (width * height) allowed in an uploaded\n     * image. This bounds the memory a single decode can allocate, guarding\n     * against decompression-bomb uploads that declare huge dimensions in a\n     * tiny file. ~24 megapixels comfortably covers legitimate photos while\n     * rejecting pathological dimensions.\n     */\n    public function getMaxResolution(): int\n    {\n        return 24_000_000;\n    }","sourceCodeStart":117,"sourceCodeEnd":153,"githubUrl":"https://github.com/flarum/framework/blob/4b939f685389bfe8a380e9e28ddf305a1c66950c/framework/core/src/Foundation/AbstractImageValidator.php#L117-L153","documentation":"AbstractImageValidator::raise converts a failed file validation rule (file required, disallowed mime type, or size over limit) into a translated 'validation.<error>' message and throws ValidationException keyed by the upload's filename. It is the base class for Flarum's avatar/logo upload validators.","triggerScenarios":"Uploading a file through endpoints validated by subclasses (e.g. UploadAvatarController, UploadLogoController) when: no file is provided, the file's mime type isn't in the allowed list (jpg/png/bmp/svg/webp variants), or the file exceeds getMaxSize() (default 2048 KiB).","commonSituations":"Users uploading PDFs or videos as avatars; profile pictures larger than the configured size limit; empty multipart uploads from broken clients.","solutions":["Upload a file whose type is one of the allowed image mimes (e.g. png, jpeg, webp) and resize it under the max size.","Raise the limits by overriding getMaxSize()/getAllowedMimes() in a subclass or extension before calling assertValid.","Handle the ValidationException in the controller/response to show the per-field translated errors to the user instead of surfacing a 500."],"exampleFix":"// before: uploading avatar.pdf\n// after: client-side guard before POST\nconst file = input.files[0];\nif (!/^image\\/(png|jpe?g|webp)$/.test(file.type) || file.size > 2 * 1024 * 1024) {\n  alert('Please choose an image under 2MB');\n  return;\n}","handlingStrategy":"validation","validationCode":"// client-side pre-check before upload\nconst allowed = ['image/png','image/jpeg','image/webp'];\nif (!allowed.includes(file.type) || file.size > 2 * 1024 * 1024) {\n  alert('Only PNG/JPEG/WebP under 2MB allowed');\n}","typeGuard":"const isUploadableImage = (f) => f instanceof File && /^image\\//.test(f.type) && f.size > 0 && f.size <= 2 * 1024 * 1024;","tryCatchPattern":"try {\n    $validator->assertValid(['file' => $uploadedFile]);\n} catch (ValidationException $e) {\n    return response()->json(['errors' => $e->errors()], 422);\n}","preventionTips":["Check allowed mime types and size limits before every upload call.","Resize/compress images client-side when over the limit.","Map the per-filename error keys to the correct form fields when displaying."],"tags":["flarum","validation","file-upload","image"],"backgroundTag":"schema-validation-failed","analyzedSha":"4b939f685389bfe8a380e9e28ddf305a1c66950c","analyzedAt":"2026-09-15T18:09:20.879Z","contentChangedAt":"2026-09-15T18:09:20.879Z","schemaVersion":2},"datasetVersion":"2026-09-16T09:17:16.951Z"}