I sent an array from my form by javascript which gets values from different input fields and then storing it in a single variable.
<input type="hidden" name="details" id="details">
js
document.querySelectorAll('form')[1].addEventListener('submit', function(event) {
var details = [];
for (var i = 0; i < {{ $event->grocery->items()->count() }}; i++) {
details[i] = [
document.querySelectorAll('.store')[i].value,
document.querySelectorAll('.item')[i].value,
document.querySelectorAll('.quantity')[i].value,
document.querySelectorAll('.brand')[i].value,
document.querySelectorAll('.size')[i].value,
];
}
document.querySelector('#details').value = JSON.stringify(details);
});
Then in my controller, I decode the array using json_decode
$request->details = json_decode($request->details);
Now, I want to validate each iteration (just to check whether it is empty or not). So, I do like this,
$request->validate([
...
'details.*.*' => 'required'
]);
But my problem is that this is not doing anything. Even if I sent an empty iteration it continues without returning an error.
Am I doing anything wrong here? Please help me.
Update
Example var dumping details
array:3 [▼
0 => array:5 [▼
0 => "Grocery"
1 => "Grocery"
2 => "Grocery"
3 => "Grocery"
4 => "Grocery"
]
1 => array:5 [▼
0 => "Grocery"
1 => "Grocery"
2 => "Grocery"
3 => "Grocery"
4 => "Grocery"
]
2 => array:5 [▼
0 => "Grocery"
1 => "Grocery"
2 => "Grocery"
3 => "Grocery"
4 => "Grocery"
]
]