1

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?

2
  • Is there a reason you named your select box item[0][]? Can't it be simply item? Commented Nov 8, 2016 at 14:56
  • It cannot be a simple string...a user can add them dynamically Commented Nov 8, 2016 at 15:44

1 Answer 1

4

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...

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.