1

I am working with the laravel validations in form request and this time I need to validate a data array something like this:

public function rules()
{
    $rules = [
       'products.*.quantity' => 'required|integer',
       'products.*.dimension' => 'required|min:5|max:16',
       'products.*.description' => 'required|min:5',
    ];

    return $rules;
}

where products is the array where I have each of the items, this works however it gives me a message more or less like this: The products.1.quantity field is required. I need to change the name of the attribute, I know it is possible to change the name inside the messages method giving a new value to products.*. quantity for example products.*. quantity => "quantity", however I would also like to specify the key of the item that is failing and at the end have a message like this:

The quantity in item 1 field is required. then is it possible to achieve this?

1

1 Answer 1

-2

Search for this file resources/lang/xx/validation.php and modify custom entry with your custom messages

'custom' => [
    'products.*.quantity' => [
        'required' => 'Your custom message',
    ]
]
Sign up to request clarification or add additional context in comments.

1 Comment

That doesn't answer the question that he posted, he wants to include the $index to the message which is returned. Your solution leaves that out, the link provided by Don't Panic, seems to be a better solution as it does handle that issue.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.