Position placeholder in validation messages

In Laravel 9 you can use the :position placeholder in validation messages if you're working with arrays.

This will output: "Please provide an amount for price #2"

1class CreateProductRequest extends FormRequest
2{
3 public function rules(): array
4 {
5 return [
6 'title' => ['required', 'string'];
7 'description' => ['nullable', 'sometimes', 'string'],
8 'prices' => ['required', 'array'],
9 'prices.*.amount' => ['required', 'numeric'],
10 'prices.*.expired_at' => ['required', 'date'],
11 ];
12 }
13 
14 public function messages(): array
15 {
16 'prices.*.amount.required' => 'Please provide an amount for price #:position'
17 }
18}

Tip given by @mmartin_joo

Like our articles?

Become a Premium Member for $129/year or $29/month
What else you will get:
  • 22 courses (477 lessons, total 38 h 20 min)
  • 2 long-form tutorials (one new every week)
  • access to project repositories
  • access to private Discord