In my application config, I have a config item that needs to be an array of values, with at least one value in it:
'names' => array_filter(explode(',', env('NAMES', ''))),
I wanted to create a validation rule for it like this:
Rule::make('migrate_from')->rules(['array', 'min:1']),
When I run config:validate
, I get this exception:
Illuminate\View\ViewException
htmlspecialchars(): Argument #1 ($string) must be of type string, array given (View: /path/to/app/vendor/ashallendesign/laravel-config-validator/resources/views/validate-config.blade.php)
at /path/to/app/vendor/illuminate/support/helpers.php:119
115โ if ($value instanceof Htmlable) {
116โ return $value->toHtml();
117โ }
118โ
โ 119โ return htmlspecialchars($value ?? '', ENT_QUOTES, 'UTF-8', $doubleEncode);
120โ }
121โ }
122โ
123โ if (! function_exists('env')) {
1 /path/to/app/vendor/illuminate/view/Engines/PhpEngine.php:60
Illuminate\View\Engines\CompilerEngine::handleViewException(Object(TypeError))
2 /path/to/app/vendor/illuminate/support/helpers.php:119
TypeError::("htmlspecialchars(): Argument #1 ($string) must be of type string, array given")
I traced this to this line in the view:
<b>{{ config($configField) }}</b>
To make it work, I replaced that line with this:
<b>{{ json_encode(config($configField, JSON_PRETTY_PRINT)) }}</b>
I'm not sure whether my fix is a good way to do it (and I would submit it as a PR), or whether there is a better way. On one hand, this way will always work and will show a nice output for all kinds of config values (including simple and associative arrays). On the other hand, it changes how regular string values appear (by putting quotes around them).
Thoughts?