Ask Your Question
1

How can the index of a field be used in the error message for Laravel array validation?

asked 2022-09-23 11:00:00 +0000

lalupa gravatar image

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
3

answered 2021-06-27 16:00:00 +0000

devzero gravatar image

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.
edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss

Add Answer


Question Tools

Stats

Asked: 2022-09-23 11:00:00 +0000

Seen: 10 times

Last updated: Jun 27 '21