← All changes
|
app/Http/Requests/GroupBulkUpdateVariantRequest.php
+17
-4
1.6.1
→
1.6.5
View file →
| @@ -1,13 +1,15 @@ | ||
| 1 | 1 | <?php |
| 2 | 2 | |
| 3 | 3 | namespace FluentCart\App\Http\Requests; |
| 4 | 4 | |
| 5 | +use FluentCart\App\Http\Rules\MaxLengthRule; | |
| 5 | 6 | use FluentCart\Framework\Foundation\RequestGuard; |
| 6 | 7 | |
| 7 | 8 | class GroupBulkUpdateVariantRequest extends RequestGuard |
| 8 | 9 | { |
| 9 | 10 | const MAX_VARIANTS_PER_REQUEST = 500; |
| 11 | + const SKU_MAX_LENGTH = 30; | |
| 10 | 12 | |
| 11 | 13 | public function rules() |
| 12 | 14 | { |
| 13 | 15 | $variantIds = $this->get('variant_ids', []); |
| @@ -13,18 +15,30 @@ | ||
| 13 | 15 | $variantIds = $this->get('variant_ids', []); |
| 14 | 16 | if (is_array($variantIds)) { |
| 15 | 17 | $variantIds = array_values(array_filter(array_map('absint', $variantIds))); |
| 16 | 18 | } |
| 17 | - $skuRule = 'nullable|sanitizeText|maxLength:30'; | |
| 18 | 19 | |
| 20 | + // maxLength is a Validator::extend()-registered custom rule, whose | |
| 21 | + // dispatcher (Validator::__call()) always uses the rule callback's | |
| 22 | + // own return value and never consults messages() — so it's passed | |
| 23 | + // as a closure carrying its own message instead of "maxLength:30". | |
| 24 | + $skuRules = [ | |
| 25 | + 'nullable', | |
| 26 | + 'sanitizeText', | |
| 27 | + 'maxLength' => MaxLengthRule::withMessage( | |
| 28 | + self::SKU_MAX_LENGTH, | |
| 29 | + esc_html__('SKU may not be greater than 30 characters.', 'fluent-cart') | |
| 30 | + ), | |
| 31 | + ]; | |
| 32 | + | |
| 19 | 33 | if (is_array($variantIds) && count($variantIds) === 1) { |
| 20 | 34 | $excludeId = absint($variantIds[0]); |
| 21 | - $skuRule .= '|unique:fct_product_variations,sku,' . $excludeId; | |
| 35 | + $skuRules[] = 'unique:fct_product_variations,sku,' . $excludeId; | |
| 22 | 36 | } |
| 23 | 37 | |
| 24 | 38 | return [ |
| 25 | 39 | 'variant_ids' => 'required|array', |
| 26 | - 'sku' => $skuRule, | |
| 40 | + 'sku' => $skuRules, | |
| 27 | 41 | ]; |
| 28 | 42 | } |
| 29 | 43 | |
| 30 | 44 | public function messages() |
| @@ -31,9 +45,8 @@ | ||
| 31 | 45 | { |
| 32 | 46 | return [ |
| 33 | 47 | 'variant_ids.required' => esc_html__('At least one variant ID is required.', 'fluent-cart'), |
| 34 | 48 | 'variant_ids.array' => esc_html__('variant_ids must be an array.', 'fluent-cart'), |
| 35 | - 'sku.maxLength' => esc_html__('SKU may not be greater than 30 characters.', 'fluent-cart'), | |
| 36 | 49 | 'sku.unique' => esc_html__('The SKU must be unique.', 'fluent-cart'), |
| 37 | 50 | ]; |
| 38 | 51 | } |
| 39 | 52 | |