1
0
mirror of https://github.com/flarum/core.git synced 2025-07-17 06:41:21 +02:00

fix: Use laravel validator to replace avatar validation error params (#2946)

This commit is contained in:
Sami Mazouz
2021-10-26 14:45:27 +01:00
committed by GitHub
parent d30bbbf847
commit 61b3539271

View File

@@ -16,6 +16,11 @@ use Symfony\Component\Mime\MimeTypes;
class AvatarValidator extends AbstractValidator class AvatarValidator extends AbstractValidator
{ {
/**
* @var \Illuminate\Validation\Validator
*/
protected $laravelValidator;
/** /**
* Throw an exception if a model is not valid. * Throw an exception if a model is not valid.
* *
@@ -23,6 +28,8 @@ class AvatarValidator extends AbstractValidator
*/ */
public function assertValid(array $attributes) public function assertValid(array $attributes)
{ {
$this->laravelValidator = $this->makeValidator($attributes);
$this->assertFileRequired($attributes['avatar']); $this->assertFileRequired($attributes['avatar']);
$this->assertFileMimes($attributes['avatar']); $this->assertFileMimes($attributes['avatar']);
$this->assertFileSize($attributes['avatar']); $this->assertFileSize($attributes['avatar']);
@@ -69,15 +76,21 @@ class AvatarValidator extends AbstractValidator
$maxSize = $this->getMaxSize(); $maxSize = $this->getMaxSize();
if ($file->getSize() / 1024 > $maxSize) { if ($file->getSize() / 1024 > $maxSize) {
$this->raise('max.file', [':max' => $maxSize]); $this->raise('max.file', [':max' => $maxSize], 'max');
} }
} }
protected function raise($error, array $parameters = []) protected function raise($error, array $parameters = [], $rule = null)
{ {
$message = $this->translator->trans( // When we switched to intl ICU message format, the translation parameters
"validation.$error", // have become required to be in the format `{param}`.
$parameters + [':attribute' => 'avatar'] // Therefore we cannot use the translator to replace the string params.
// We use the laravel validator to make the replacements instead.
$message = $this->laravelValidator->makeReplacements(
$this->translator->trans("validation.$error"),
'avatar',
$rule ?? $error,
array_values($parameters)
); );
throw new ValidationException(['avatar' => $message]); throw new ValidationException(['avatar' => $message]);