'Laravel form validtor with array field and numeric validation
I am making a form having 2 fields unit_id and price.
<select name="unit_id[]">...</select>
<input name="price[]" />
Note: I have 5 sets of the above 2 lines in the form.
On the other hand my validator looks like this -
$request->validate([
'price' => 'required|array',
'price.*' => 'numeric|min:0|max:9999',
'unit_id' => 'required|array',
]);
Now I am getting this error, due to the numeric validation -
TypeError
htmlspecialchars(): Argument #1 ($string) must be of type string, array given
Request POST dump looks like this -
array:2 [▼
"price" => array:2 [▼
0 => "20"
1 => "abcd"
]
"unit_id" => array:2 [▼
0 => "1"
1 => "4"
]
]
Any idea about how to validate and force each price field to be of numeric?
Solution 1:[1]
this problem is for the value of the input and maybe when you use blade elements in the dom element the content has the wrong structure and made this problem. in some cases, double quotes can't convert correctly when using json_encode or json_decode, and cause of this error.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | ali katiraie |
