'Localized validation error messages escaping UTF-8 characters
I'm using Laravel 9, and the strangest issue regarding localization (and escaped UTF-8) happens in a web (not api) route.
I try and validate some password input in my controller:
protected function validatePassword($pwd) {
$pwdRules = ['required', Password::min(8)->letters()->mixedCase()->numbers()->symbols()];
$validation = Validator::make(['password' => $pwd], ['password' => $pwdRules]);
if ($validation->fails())
return ['valid' => false, 'data' => $validation->errors()];
return ['valid' => true, 'data' => $validation->validated()];
}
The caller function pushes it into an array and passes it to the view:
$newPwdValidatedAnalysis = $this->validatePassword($newPwd);
if (!$newPwdValidatedAnalysis['valid']) {
$errors[] = 'A senha nova não atende às exigências.'; // This was hardcoded
$errors[] = $newPwdValidatedAnalysis['data']; // These messages will come from a localization file app\lang\pt-BR\pt-BR.json
}
The blade view renders it as follows:
A senha nova não atende às exigências.
{"password":["O campo senha deve ter pelo menos 8 caracteres.","O campo senha deve conter pelo menos um s\u00edmbolo."]}
It puzzles me so much, because the same variable countains a hardcoded message--which is UTF-8 unescaped, thus displayed correctly--and a Json-parsed localization message--which is UTF-8 escaped.
The UTF-8 escaping seems to happen whenever Laravel interacts with a Json file. In my API I was able to get around this issue this way (viz., adding JSON_UNESCAPED_UNICODE as an 4th parameter to the json() function).
What could I do to make Laravel display unescaped Unicode by default on a web route when the data is read from a Json? The workaround is to define custom validation messages using $this->validate($request, $rules, $customMessages) (as they come from an array, they are correctly displayed), but it's really annoying to think of this as a strange elefant-bug in Laravel.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
