kristijanhusak / laravel-form-builder

Laravel Form builder for version 5+!
https://packagist.org/packages/kris/laravel-form-builder
MIT License
1.69k stars 295 forks source link

Values ​​of translated fields #451

Open matteocastiglioni opened 6 years ago

matteocastiglioni commented 6 years ago

Hi @kristijanhusak, thanks for your work with this package! It's awesome!

I'm using https://github.com/spatie/laravel-translatable to make eloquent models translatable. Translations are stored as json. Once the translatable trait is added on model, the field values are retrieved automatically based on the current app language.

When i try to repopulate an edit form, i get the following error

Method Illuminate\View\View::__toString() must not throw an exception, caught ErrorException: htmlspecialchars() expects parameter 1 to be string, array given

as a very quick fix i've edited the function setValue in Kris\LaravelFormBuilder\Fields adding the following check if (is_array($value)) $value = isset($value[app()->getLocale()]) ? $value[app()->getLocale()] : ''

Is it possible to make the package take into consideration the translatable trait when the field values are extracted?


Laravel framework 5.6.33 Laravel form builder 1.15.1

matteocastiglioni commented 6 years ago

After digging a little deeper i've found that the error occur only with named forms. In the getModelValueAttribute function, the model is seen as an array and the values ​​of the translated fields are extracted as array.