Validation with Internationalization in Laravel 5.3 error

Asked

Viewed 659 times

1

I am creating an application where I have to validate the data sent to the database (obvious), however, I am responding to the following error:

Errorexception in Fileloader.php line 109:

Object of class Illuminate Routing Router could not be converted to string

So that you can understand better I will present my code:

Routes/web.php

...
Route::group(['prefix' => 'dashboard/{locale}/'], function($locale) {

    if (empty($locale)) {
        $locale = 'pt-BR';
    }

    App::setLocale($locale);

    ...
    Route::get('api/add', 'ApiController@addApi');
    Route::post('api/add', 'ApiController@storeApi');

    ...
}

Example of URL: /dashboard/pt-BR/api/add

app/Http/Controllers/Apicontroller.php

public function storeApi(Request $request)
{
    $validator = Validator::make(
        $request->all(),
        $this->rules, 
        $this->messages
    )->validate(); // Por padrão, em caso de erro, ele retornaria para o form com os erros.

    $api = $this->apiRepository->model()->create($request->all());
    
    // $this->locale = 'pt-BR' - É recuperado por um middleware...
    if ($api) {
        return redirect()->to(
            'dashboard/' . $this->locale .
            '/api/add-accounting/' . $api['id']
        );
    }
}

One thing I observed

I stopped the path presented in error, being it:

\path\vendor\laravel\framework\src\Illuminate\Translation\FileLoader.php
Linha: 109

And that’s the method:

protected function loadPath($path, $locale, $group)
{
    if ($this->files->exists($full = "{$path}/{$locale}/{$group}.php")) {
        return $this->files->getRequire($full);
    }

    return [];
}

If I put one dd() in each parameter, I have:

$path = 'path\resources\lang'
$locale = Router {#24 ▼
              #events: Dispatcher {#5 ▶}
              #container: Application {#3 ▶}
              #routes: RouteCollection {#26 ▶}
              #current: Route {#230 ▶}
              #currentRequest: Request {#40 ▶}
              #middleware: array:6 [▶]
              #middlewareGroups: array:2 [▶]
              +middlewarePriority: array:5 [▶]
              #binders: array:1 [▶]
              #patterns: []
              #groupStack: []
          }
$group = 'validation'

I particularly think the problem must be in $locale but I could not identify precisely which error, because I forced the same to $locale = "pt-BR" and the error changes to:

Errorexception in Translator.php line 273:

Illegal offset type

I imagined that it could be error exactly in the translation files, however, I saw the same and it is also all right, however, for any doubt, follow the same on github.

  • Have you tried using this package here ? https://packagist.org/packages/mcamara/laravel-localization

  • Where does Intl enter? it is enabled?

  • Yes, Intl is enabled and if I remove the validation, everything works. The problem is when I try to validate the fields and returns an error.

1 answer

1


The problem is how you define your Route::group. This method is different from those used with Route::get, Route::post and the like.

Although it also receives an anonymous function, the first argument received is an object of the type Illuminate\Routing\Router (Notice what returns in your dd).

Something that can help you with what you want to do is to use the Sub-Domain Routing:

Route::group(['domain' => '{locale}.myapp.com', 'prefix' => 'dashboard/'], function () {

    Route::get('user/{id}', function ($locale, $id) {
        // Ao acessar pt-br.myapp.com/dashboard/user/1
        echo $locale; // pt-br
    });

});

This implies to change a little the structure of your routes, but in my view it is a simpler way to achieve what you want.

From there you can set the app()->setLocale() from a middleware global for example.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.