From 1d5b48b88d4d18a5bcaedbabf00da56bee6c72d7 Mon Sep 17 00:00:00 2001 From: Marcus Moore Date: Thu, 25 Apr 2024 16:22:03 -0700 Subject: [PATCH] Add comment and improve method --- app/Http/Requests/StoreAssetRequest.php | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/app/Http/Requests/StoreAssetRequest.php b/app/Http/Requests/StoreAssetRequest.php index 38d332562c..fb23ac8f2f 100644 --- a/app/Http/Requests/StoreAssetRequest.php +++ b/app/Http/Requests/StoreAssetRequest.php @@ -47,14 +47,17 @@ class StoreAssetRequest extends ImageUploadRequest { $modelRules = (new Asset)->getRules(); - $modelRules['purchase_cost'] = $this->removeNumericRulesFromPurchaseCost($modelRules['purchase_cost']); + // If purchase_cost was submitted as a string with a comma separator + // then we need to ignore the normal numeric rules. + // Since the original rules still live on the model they will be run + // right before saving (and after purchase_cost has been + // converted to a float via setPurchaseCostAttribute). + $modelRules = $this->removeNumericRulesFromPurchaseCost($modelRules); - $rules = array_merge( + return array_merge( $modelRules, parent::rules(), ); - - return $rules; } private function parseLastAuditDate(): void @@ -74,15 +77,19 @@ class StoreAssetRequest extends ImageUploadRequest } } - private function removeNumericRulesFromPurchaseCost($purchaseCost) + private function removeNumericRulesFromPurchaseCost(array $rules): array { + $purchaseCost = $rules['purchase_cost']; + // If rule is in "|" format then turn it into an array if (is_string($purchaseCost)) { $purchaseCost = explode('|', $purchaseCost); } - return array_filter($purchaseCost, function ($rule) { + $rules['purchase_cost'] = array_filter($purchaseCost, function ($rule) { return $rule !== 'numeric' && $rule !== 'gte:0'; }); + + return $rules; } }