feat: enhance product and raw material request validation by adding imagesRequired parameter, improving image handling based on request method
This commit is contained in:
parent
e431cbb54b
commit
c8344f27cb
@ -32,7 +32,10 @@ public function rules(): array
|
||||
'category_ids' => ['required', 'array', 'min:1'],
|
||||
'category_ids.*' => ['integer', Rule::exists('categories', 'id')->whereNull('deleted_at')],
|
||||
|
||||
...$this->productVariantRules(productId: $this->route('product')?->id),
|
||||
...$this->productVariantRules(
|
||||
productId: $this->route('product')?->id,
|
||||
imagesRequired: $this->isMethod('POST'),
|
||||
),
|
||||
];
|
||||
}
|
||||
|
||||
|
||||
@ -30,7 +30,10 @@ public function rules(): array
|
||||
'name' => ['required', 'string', 'max:200'],
|
||||
'unit' => ['required', Rule::enum(RawMaterialUnit::class)],
|
||||
|
||||
...$this->rawMaterialPriceRules(rawMaterialId: $this->route('rawMaterial')?->id),
|
||||
...$this->rawMaterialPriceRules(
|
||||
rawMaterialId: $this->route('rawMaterial')?->id,
|
||||
imagesRequired: $this->isMethod('POST'),
|
||||
),
|
||||
];
|
||||
}
|
||||
|
||||
|
||||
@ -16,7 +16,7 @@ trait HasProductVariantRules
|
||||
/**
|
||||
* @return array<string, mixed>
|
||||
*/
|
||||
protected function productVariantRules(string $variantsKey = 'variants', ?int $productId = null): array
|
||||
protected function productVariantRules(string $variantsKey = 'variants', ?int $productId = null, bool $imagesRequired = true): array
|
||||
{
|
||||
$isAdminBahanBaku = $this->user()?->hasRole(Role::ADMIN_BAHAN_BAKU->value) ?? false;
|
||||
|
||||
@ -25,7 +25,7 @@ protected function productVariantRules(string $variantsKey = 'variants', ?int $p
|
||||
"{$variantsKey}.*.name" => ['required', 'string', 'max:200'],
|
||||
"{$variantsKey}.*.stock" => ['required', 'integer', 'min:0'],
|
||||
"{$variantsKey}.*.retail_stock" => ['required', 'integer', 'min:0'],
|
||||
...$this->variantImageRules($variantsKey, required: true),
|
||||
...$this->variantImageRules($variantsKey, required: $imagesRequired),
|
||||
];
|
||||
|
||||
if ($productId) {
|
||||
|
||||
@ -11,14 +11,14 @@ trait HasRawMaterialPriceRules
|
||||
/**
|
||||
* @return array<string, mixed>
|
||||
*/
|
||||
protected function rawMaterialPriceRules(string $pricesKey = 'prices', ?int $rawMaterialId = null): array
|
||||
protected function rawMaterialPriceRules(string $pricesKey = 'prices', ?int $rawMaterialId = null, bool $imagesRequired = true): array
|
||||
{
|
||||
$rules = [
|
||||
"{$pricesKey}" => ['required', 'array', 'min:1'],
|
||||
"{$pricesKey}.*.variant" => ['required', 'string', 'max:200'],
|
||||
"{$pricesKey}.*.price" => ['required', 'integer', 'gt:0'],
|
||||
"{$pricesKey}.*.stock" => ['required', 'numeric', 'decimal:0,4', 'min:0'],
|
||||
...$this->variantImageRules($pricesKey, required: true),
|
||||
...$this->variantImageRules($pricesKey, required: $imagesRequired),
|
||||
];
|
||||
|
||||
if ($rawMaterialId) {
|
||||
|
||||
Loading…
Reference in New Issue
Block a user