1

My Laravel form submission has an item called invoice_products which contains all lines of invoice data.Here is what I get when I dd($request->invoice_products) . It is an array of two dynamically generated forlm lines. I can have many such lines in my form. I am trying to validate each of these fields individually.

 [
  {
    "product_no": 3,
    "product_name": "Dolor vitae ducimus voluptatum quia.",
    "product_price": 12,
    "product_disc": 0,
    "product_qty": 1,
    "line_total": "12.00",
    "product_tax": {
      "rate": 5,
      "name": "VAT"
    },
    "line_tax_value": "0.60",    
    "product_sku": "Dr. Bailee Aufderhar MD"
  },
  {
    "product_no": 13,
    "product_name": "Esse explicabo quos ut autem iusto nam est quia.",
    "product_price": 111,
    "product_disc": 0,
    "product_qty": 1,
    "line_total": "111.00",
    "product_tax": {
      "rate": 5,
      "name": "VAT"
    },
    "line_tax_value": "5.55",    
    "product_sku": "Addie Reichert II"
  }
]

This is my validation code in laravel. It doesn't seem to work. What am I missing? I read in the documentation that arrays can be validated using *.

     $this->validate($request, [
            'invoice_products.product_sku.*' => 'required|exists:items,sku',
            'invoice_products.product_price.*' => 'required',
            'invoice_products.product_disc.*' => 'required',
            'invoice_products.product_qty.*' => 'required|numeric|min:0|regex:/^\d+(\.\d{1,2})?$/',
    ]);

1 Answer 1

1

You've got the asteric in the wrong place, try:

'invoice_products.*.product_sku' => ...
Sign up to request clarification or add additional context in comments.

3 Comments

Well that's the correct syntax according to the official docs.
Also, invoice_products.product_rate doesn't exist in your array. Did you mean invoice_products.*.product_tax.rate?
Sorry, that should have been {{invoice_products.product_price}} . My mistake.

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.