Laravel验证输入数组 [英] Laravel Validate Array of Inputs
问题描述
我有一个表格:
<input type="text" name="name[1]">
<input type="text" name="name[2]">
<input type="text" name="name[3]">
<input type="submit" value="Submit">
我已经创建了带有规则的表单验证文件:
I've created form validation file with rules:
class formRequest extends FormRequest {
....
public function rules()
{
return ['name.*' => 'unique:names'];
}
public function messages()
{
return ['name.unique' => 'Name is already in DB!'];
}
在数据库中已经存在的输入(例如name[1]
)中提交带有值(例如'John')的表单后,我得到:
After submitting the form with value (e.g. 'John') in input (e.g. name[1]
), which already exists in the DB, I get:
SQLSTATE [42S22]:找不到列:1054"where子句"中的未知列"name.1"(SQL:从"names"中选择count(*)作为集合,其中"name".`1`= John)
SQLSTATE[42S22]: Column not found: 1054 Unknown column 'name.1' in 'where clause' (SQL: select count(*) as aggregate from `names` where `name`.`1` = John)
因此,将name.*
视为创建另一个字段名,而不是遍历数组.
So it's treating name.*
as for creating another field name, instead of looping through the array.
我的Laravel Framework是5.4.19版.根据文档,name.*
应该在验证过程中遍历数组
My Laravel Framework is version 5.4.19. According to the docs, the name.*
should work to iterate through an array during validation.
我做错了什么?
推荐答案
,Laravel将添加输入名称,在您的情况下,输入名称为一个数字.
by default if you didn't provide a column name for the unique
rule, Laravel will add the input name, and in your case the input name is a number.
要解决此问题,请将规则更改为以下内容:
to fix this issue change the rule to the following instead:
return ['name.*' => 'unique:names,name'];
这篇关于Laravel验证输入数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!