K
K
Konstantin Eliseev2019-07-15 16:19:45
Laravel
Konstantin Eliseev, 2019-07-15 16:19:45

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',
        ];
    }
}

UPDATE: Did this:
$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

1 answer(s)
J
JhaoDa, 2019-07-15
@K0r5hun

https://laravel.com/docs/5.8/validation#custom-val...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question