I'm creating my first package for lumen 6.x. I'm able to use my own translated messages from /resources/lang/es/messages.php
within my package but for some reason, validation messages are not working, in fact, the /resources/lang/es/validation.php
is ignored.
I'm guessing that's something related to the way that validator is implemented in the controller since translations are correctly loaded from my Provider, therefore I assume that it's being loaded too.
My /resources/lang/es/validation.php
looks like:
<?php
return [
'required' => 'El campo :attribute es obligatorio.',
];
My boot function within service provider:
public function boot()
{
$this->loadTranslationsFrom(__DIR__.'/../resources/lang', 'locations');
$this->loadMigrationsFrom(__DIR__.'/../database/migrations');
$this->loadRoutesFrom(__DIR__.'/../routes/web.php');
// Publishing is only necessary when using the CLI.
if ($this->app->runningInConsole()) {
$this->bootForConsole();
}
}
The store function in Controller
public function store(Request $request)
{
//
$validator = Validator::make($request->all(), [
'name' => 'required'
]);
if ($validator->fails()) return $validator->errors();
$resource = Country::create($request->toArray());
return response()->json([
'message' => __('locations::messages.store_success'),
'resource' => $resource
]);
}
Also in my main .env
lumen file I've added APP_LOCALE=es
which works perfect for translations.
So the problem is that I'm not able to show translated message for required name
field.
BTW, con can look the whole code in my testing git repository (Please ignore States and Cities stuff since it under development). https://github.com/Imboga/Locations
Thanks in advance
Edit: I also tried with this, but no luck so far
public function store(Request $request)
{
//
$this->validate($request, [
'name' => 'required'
]);
$resource = Country::create($request->toArray());
return response()->json([
'message' => __('locations::messages.store_success'),
'resource' => $resource
]);
}
I just found a solution. For those facing the same issue, what I've done is to add third and fourth parameters in validate()
method referencing the localized file:
public function update(Request $request)
{
//
$this->validate($request, [
'name' => 'required'
], trans('locations::validation'), trans('locations::validation.attributes') );
$resource = Country::findOrFail($request->id);
$resource->update($request->toArray());
return response()->json([
'message' => __('locations::messages.update_success'),
'resource' => $resource
]);
}
The fourth parameter has to point to the attributes array within validation.php
.
On the other hand, with this change, I'm able to overwrite my package validation.php
file in main app by adding a validation.php
in resources/lang/vendor/locations/
FYI, my validation.php
looks like:
<?php
return [
'required' => 'El campo :attribute es obligatorio.',
'attributes' => [
'name' => 'Nombre'
],
];
Not sure if this is the best approach, but it's working so far. Any improvement will be appreciated.