Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

To use the index of a field in the error message for Laravel array validation, you can use the :key placeholder in the message. For example, if you have a validation rule for an array items with a minimum length of 2:

$rules = [
    'items.*' => 'required|min:2',
];

$messages = [
    'items.*.min' => 'The :attribute at index :key must have a minimum length of :min.',
];

In the error message, the :attribute placeholder will be replaced by the name of the field (items in this case), and the :key placeholder will be replaced by the index of the invalid item. For example, if the second item in the array is invalid, the error message might be:

The items at index 1 must have a minimum length of 2.