Search code examples
phplaravellaravel-5.3

htmlspecialchars() expects parameter 1 to be string, array given


I have a form and if i submit the form with all the right data everything goes perfectly fine... but if I intentionally make any flaw

(validation for example 'title' => 'required|min:2')

and I put only one character for title or if I miss any required field I get this error:

htmlspecialchars() expects parameter 1 to be string, array given

enter image description here

I have figured out that the problem is with this select box

{!! Form::select('item[0][]', $items, null, ['class' => 'form-control', 'required']) !!}

and I even tried to use a normal select box without form helper {!! !!}

But I still get the same error!

So the problem is somewhere with validation when there is a nested array....is there a way to fix this?


Solution

  • OK I finally have an answer for this problem....it seems like something has changed in Laravel 5.3 and if you want to have a name with array like this

    {!! Form::label('title', '* Eventname: ', ['class' => 'control-label']) !!}
    {!! Form::text('title[]', null, ['class' => 'form-control', 'required') !!}
    

    You must put [0] something in brackets 'indices' like this:

    {!! Form::text('title[0]', null, ['class' => 'form-control', 'required') !!}
    

    and then in validation use

    title.*

    for rule

    UPDATE

    Because i use dynamic form that can be expanded and new form fields added (optionally) i needed to put [] array notation for a name but actually if you already have hard coded many fields with the same name like item[] you don't have to put [0] indices inside. The validation will work for them.

    The problem comes only if you have a single input field and you put [] array notation along the name for example 'item[]'

    this will trigger the error if any validation rule is broken...