laravel htmlspecialchars() 期望参数 1 是字符串,给定数组

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/40489974/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 14:42:56  来源:igfitidea点击:

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

phplaravellaravel-5.3

提问by lewis4u

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')

(例如验证'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?

因此,当存在嵌套数组时,问题出在验证的某个地方……有没有办法解决这个问题?

回答by lewis4u

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

好的,我终于有了这个问题的答案......似乎在 Laravel 5.3 中发生了一些变化,如果你想要一个像这样的数组名称

{!! 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:

您必须像这样将 [0] 放在括号“索引”中:

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

因为我使用可以扩展的动态表单并添加了新的表单字段(可选)我需要为名称放置 [] 数组符号,但实际上如果您已经硬编码了许多具有相同名称的字段,例如 item[] 您不需要必须把 [0] 索引里面。验证对他们有用。

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

仅当您有一个输入字段并且在名称中放置 [] 数组符号时才会出现问题,例如 'item[]'

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

如果任何验证规则被破坏,这将触发错误......