Anonymous Blade Component – Rendering slot variable to function yields array_key_exists(): The first argument should be either a string or an integer

  laravel, laravel-blade

Laravel 7.X

I’ve created a simple anonymous input blade component that includes validation error output. It was a translation of a previously working template.

The component is as follows:

<div class="form-group">
    <label for='{{$field}}'>{Anonymous Blade Component - Rendering slot variable to function yields array_key_exists(): The first argument should be either a string or an integer} @error($field) - <span class='text-danger'> {{ $message }} </span> @enderror </label>
    <input id='{{$field}}' name='{{$field}}' type="text" class='form-control{{ $errors->has($field) ? ' is-invalid' : '' }}' placeholder='{{$placeholder}}' {{$attributes}} value='{{ old($field, $value) }}' >
</div>

In the view, I do the following:

<x-input-validation autofocus onblur="validate()">
    <x-slot name="field">name</x-slot>
    <x-slot name="title">Name</x-slot>
    <x-slot name="placeholder">Add Name</x-slot>
    <x-slot name="value">{{$app->name}}</x-slot>
</x-input-validation>

During rendering, I receive the following error message:

array_key_exists(): The first argument should be either a string or an integer (View: C:wamp64wwwDEresourcesviewscomponentsinput-validation.blade.php)

The problem seems to be related to the @error($field) directive and the old($field, $value) function. If I change these to remove the $field variable, it will render.

It also seems to work, unaltered, if there are no validation errors present, or if there aren’t previous values stored in the session. It renders properly under this situation, too.

After many tries, one tidbit seems to be that the $field variable is of type HTMLString. Not sure if this is a red herring or not, but given it’s complaining about type, I’m assuming that’s the source of the problem.

Any ideas how I should correct this issue? I’m stumped.

Thanks!

Source: Laravel

Leave a Reply