I have a problem with validation in Laravel. It used to work properly but now it does not. I do not know when the problem started.
The problem appears using the Validator class or a custom request file.
For example:
$validator = Validator::make($request->all(), [
'username' => 'required',
]);
If username
is missing will produce an error bag containing the following error text:
validation.required
which is useless as far as validation feedback goes. If another field does not satisfy the minimum length, the error bag will contain:
validation.required
validation.min
Instead it should use the descriptions in the lang/en/validation.php
file: 'The :attribute field is required.'
i.e. 'The username field is required.'
It seems like the lang file is completely ignored. I checked it, and it contains a valid array. I don't know how to troubleshoot this. Can you please help?
I print out the errors as follows:
@if ($errors->any())
<div >
<ul>
@foreach ($errors->all() as $message)
<li>{{ $message }}</li>
@endforeach
</ul>
</div>
@endif
Example of message bag as seen from the debugger:
UPDATE: I found the original cause of the problem.
# vendor/laravel/framework/src/Illuminate/Translation/Translator.php
public function get( ..
this function fails to retrieve the translated line. Because the load() function fails to load the contents of lang/en/validation.php Because it's looking for it in the wrong path (/resources/lang/). Because there exists a /resource/lang folder, which was put there by a third party package (outdated, that was the path in laravel 8).
CodePudding user response:
The problem was in fact with the translation subsystem.
It's been caused by a third party plugin which created a /resources/lang folder. This was the lang folder location in Laravel 8, and it still takes precedence over the new location /lang. But the necessary files were not there and the translation failed without raising any exception (!!!).
Removing the /resources/lang folder resolved my issue.