Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

alihoushyaripour's avatar

How to validate count of array objects in request class?

Hi,

I have a request class that validate an array of categories that each one of them have an id

Now I want to check count of received categories and limited to 3, how can I check it?

Request class:

class PlaceCategory extends FormRequest
{
    /**
     * Get the validation rules that apply to the request.
     *
     * @return array
     */
    public function rules()
    {
        return [
            'categories' => 'required|array',
            'categories.id' => 'required|numeric|integer|regex:/^[0-9]+$/',
        ];
    }
}
0 likes
2 replies
Nash's avatar

'categories' => 'required|array|max:3',

1 like
mballaag's avatar

you can use

'categories' => 'required|array|between:1;3',

1 like

Please or to participate in this conversation.