From 972411673fe33876bf7eddcabea8270e17ab10a8 Mon Sep 17 00:00:00 2001 From: Sami Mazouz Date: Tue, 26 Oct 2021 14:45:27 +0100 Subject: [PATCH] fix: Use laravel validator to replace avatar validation error params (#2946) --- src/User/AvatarValidator.php | 23 ++++++++++++++++++----- 1 file changed, 18 insertions(+), 5 deletions(-) diff --git a/src/User/AvatarValidator.php b/src/User/AvatarValidator.php index 84d8e0995..f9689b239 100644 --- a/src/User/AvatarValidator.php +++ b/src/User/AvatarValidator.php @@ -16,6 +16,11 @@ use Symfony\Component\Mime\MimeTypes; class AvatarValidator extends AbstractValidator { + /** + * @var \Illuminate\Validation\Validator + */ + protected $laravelValidator; + /** * Throw an exception if a model is not valid. * @@ -23,6 +28,8 @@ class AvatarValidator extends AbstractValidator */ public function assertValid(array $attributes) { + $this->laravelValidator = $this->makeValidator($attributes); + $this->assertFileRequired($attributes['avatar']); $this->assertFileMimes($attributes['avatar']); $this->assertFileSize($attributes['avatar']); @@ -69,15 +76,21 @@ class AvatarValidator extends AbstractValidator $maxSize = $this->getMaxSize(); 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( - "validation.$error", - $parameters + [':attribute' => 'avatar'] + // When we switched to intl ICU message format, the translation parameters + // have become required to be in the format `{param}`. + // 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]);