What seems to be the problem:
Hi,
I’m using Real-time validation on a form on a product page, for creating a new Product.
The problem is the sale_price field validation is set to numeric, but it is not required. However, when I fill out the form and leave the field empty, it still shows the validation error:
The sale price must be a number.
AdminAddProductComponent.php:
public function updated($fields)
{
$this->validateOnly($fields, [
'name' => 'required',
'slug' => 'required|unique:products',
'short_description' => 'required',
'description' => 'required',
'regular_price' => 'required|numeric',
'SKU' => 'required',
'stock_status' => 'required',
'quantity' => 'required|numeric',
'image' => 'required|mimes:jpeg,png,jpg',
'category_id' => 'required'
]);
}
public function addProduct()
{
$this->validate([
'name' => 'required',
'slug' => 'required|unique:products',
'short_description' => 'required',
'description' => 'required',
'regular_price' => 'required|numeric',
'sale_price' => 'numeric',
'SKU' => 'required',
'stock_status' => 'required',
'quantity' => 'required|numeric',
'image' => 'required|mimes:jpeg,png,jpg',
'category_id' => 'required'
]);
$product = new Product();
$product->name = $this->name;
$product->slug = $this->slug;
$product->short_description = $this->short_description;
$product->description = $this->description;
$product->regular_price = $this->regular_price;
$product->sale_price = $this->sale_price;
$product->SKU = $this->SKU;
$product->stock_status = $this->stock_status;
$product->featured = $this->featured;
$product->quantity = $this->quantity;
$product->category_id = $this->category_id;
$imageName = Carbon::now()->timestamp. '.' . $this->image->extension();
$this->image->storeAs('products', $imageName);
$product->image = $imageName;
$product->save();
session()->flash('message', 'Product has been created successfully!');
}