Im developing inventory system using laravel.
Everything were going as expected until im facing a problem on form validation.
So below is my insert function in my order controller code,
public function insert(Request $request) {
// Save data to "orders" table
$orderer = [
'orderId' => $request->get('orderId'),
'customerId' => $request->get('customerId'),
'orderTotal' => $request->get('orderTotal'),
'paymentMethodId' => $request->get('paymentMethodId'),
'orderStatusId' => $request->get('orderStatusId'),
'created_at' => \Carbon\Carbon::now(), # \Datetime()
'updated_at' => \Carbon\Carbon::now() # \Datetime()
];
// Looping on dynamic "New Added" input field
$orderedItems_Sku = $request->get('productSku');
foreach($orderedItems_Sku as $key => $v)
{
$productSku = $request->get('productSku') [$key];
$product = \App\Product::find($productSku);
$rules = [
'orderId' => 'unique:orders',
'customerId' => 'required',
'orderStatusId' => 'required',
'paymentMethodId' => 'required',
'uomId.*' => 'required',
'productSku.*' => 'required',
'productQty.*' => 'required',
// 'lte' means Less Than or Equal to available stock quantity
'orderQty.*' =>
'required|numeric|min:1|lte:'.$product['productQty'],
'orderPrice.*' => 'required'
];
$messages = [
'customerId.required' => 'Please select Customer',
'orderStatusId.required' => 'Please select Order Status',
'paymentMethodId.required' => 'Please select Payment Method',
'uomId.*.required' => 'Please select UOM',
'productSku.*.required' => 'Please select Product',
'orderQty.*.required' => 'Please enter Order Quantity',
'orderQty.*.min' => 'Quantity must be at least 1',
'orderQty.*.lte' => 'Quantity of product no.
'.$product['productSku'].' must be not greater than
'.$product['productQty'],
'orderPrice.*.required' => 'Please enter Product Price'
];
// Validate "Rules" and "Messages" to running.
$this->validate($request, $rules, $messages);
$data = [
'orderId' => $request->get('orderId'),
'productSku' => $v,
'orderQty' => $request->get('orderQty') [$key],
'uomId' => $request->get('uomId'),
'orderPrice' => $request->get('orderPrice') [$key],
'orderDiscount' => $request->get('orderDis') [$key],
'orderPerAmount' => $request->get('orderPerAmount') [$key],
'created_at' => \Carbon\Carbon::now(), # \Datetime()
'updated_at' => \Carbon\Carbon::now() # \Datetime()
];
\App\orderedItem::insert($data);
// "Decrement" means, product quantity will be deducted based on how
much quantity that customer has ordered.
\App\Product::where("productSku", $v)->decrement("productQty",
$request->orderQty [$key]);
}
\App\Order::insert($orderer);
return redirect('orders')->with('success', 'Order Successfully Added!');
}
Here's is my html code inside create.blade.php,
as you can see, there's "orderQty" and "productQty" field.
So, im trying to validate the dynamic "orderQty" field with the dynamic "productQty" field to prevent user from entering quantity greater than value inside "productQty" field.
im also using lte (less than equal to) command inside $rules array in my code as per recommended by most of laravel communities.
This thing only works perfectly on single input but not on dynamically added input. Refer to the image below, you can see, the second input of the product not validated properly as i wanted.
even the error messages not showing the dynamic message as wanted.
Please help me to find out what is the problem. I have trying so many solutions but still not working. :'(