Answer the question
In order to leave comments, you need to log in
How to compare number of array elements with each other in validation?
There are 2 fields with multiple filled values. How can it be compared that the number of elements in the first array is equal to the number of elements in the second array?
class MyRequest extends FormRequest
{
public function rules(): array
{
return [
// 'categories' => 'required|same:titles', // Тут бы equal:field какой-нибудь... Нашёл только qte:field и lte:field
'categories.*' => 'required|string|exists:categories,slug',
'titles.*' => 'required|string|max:255',
];
}
}
$rules = [
'categories' => 'required|array|min:1',
'titles' => 'required|array|min:1',
];
foreach($this->request->get('categories') as $key => $val) {
$rules['categories.' . $key] = 'required|integer|exists:categories,id';
$rules['titles.' . $key] = 'required|string|max:255';
}
return $rules;
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question