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 7ebf535b25
commit 972411673f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -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]);