title: Laravel validation a field in an array
body: <p>I'm building a farming app with Laravel and Vue. There are several activities that happen everyday in the farm and an activity might need to use products (fertilizers). In the Create.vue Activity page I'm generating multiple html inputs. A <code>select</code> to select a product an <code>input</code> to enter the quantity so I end up having a <code>selected_products</code> array in my request. Here's a data sample when I <code>dd($request-&gt;all)</code></p>
  <p><a href="https://i.stack.imgur.com/F4NPK.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/F4NPK.png" alt="enter image description here" /></a></p>
  <p><strong>What is the problem?</strong></p>
  <p>The quantity entered in the <code>selected_products</code> array must be less than or equal to the quantity in stock. (can't have a product with id 9 with quantity 10 (liters or kilograms) when I only have 5 (liters or kilograms) in stock for that product)</p>
  <p>So <code>Product</code> and <code>Stock</code> are <code>one-to-one</code> relationship</p>
  <p>Product.php</p>
  <pre><code>public function stock () {
      return $this-&gt;morphOne(Stock::class, 'stockable');
  }
  </code></pre>
  <p>(I'm using morphOne here 'cause stock can also include the fuel quantity for tractors just in case you're curious)</p>
  <p>Anyways I just want the validation to <code>fail</code> and return an <strong>Out of stock</strong> if the quantity in stock for that very product is less than the quantity entered</p>
  <p>By the way after the validation I'm doing this sort of thing in my ActivityController <code>store</code> method so I can attach date in the <code>activity_product</code> table</p>
  <pre><code>attach_product_data = [];
  foreach ($request-&gt;input('selected_products') as $product) {
      $attach_product_data[$product['id']] = ['quantity' =&gt; $product['quantity']];
  }
  
  $activity-&gt;products()-&gt;attach($attach_product_data);
  </code></pre>
  <p>And this is what I'm trying to accomplish here but I have no idea how</p>
  <p>StoreActivityRequest.php</p>
  <pre><code>public function rules()
  {
      return [
          'selected_products.*.id' =&gt; ['nullable'],
          'selected_products.*.quantity' =&gt; ['required_with:selected_products.*.id',
  
      function () {
         /* first we should find the product with the 
         `selected_products.*.id` id and check if the quantity in stock is 
         greater than or equal to the quanity in 
         `selected_products.*.quantity` */
      }],
      ];
  }
  </code></pre>