Laravel Validate Array of inputs

I have a form:

<input type="text" name="name[1]">
<input type="text" name="name[2]">
<input type="text" name="name[3]">
<input type="submit" value="Submit">

      

I created a 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!'];
}

      

After submitting a form with a value (for example "John") in an input (for example name[1]

) that already exists in the DB, I get:

SQLSTATE [42S22]: Column not found: 1054 Unknown column 'name.1' in 'where clause' (SQL: select count (*) as a collection from `names` where` name`.`1` = John)

So it handles name.*

both to create a different field name, not to loop through the array.

My Laravel Framework is version 5.4.19. According to the docs , name.*

should work for iterating over the array during validation.

What am I doing wrong?

+3


source to share


1 answer


by default, if you didn't provide a column name for the rule unique

, 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:



return ['name.*' => 'unique:names,name'];

      

+2


source







All Articles