← All changes
|
app/Http/Requests/GroupBulkUpdateVariantRequest.php
+30
-0
1.5.0
→
1.6.5
View file →
| @@ -1,18 +1,44 @@ | ||
| 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 | { |
| 15 | + $variantIds = $this->get('variant_ids', []); | |
| 16 | + if (is_array($variantIds)) { | |
| 17 | + $variantIds = array_values(array_filter(array_map('absint', $variantIds))); | |
| 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 | + | |
| 33 | + if (is_array($variantIds) && count($variantIds) === 1) { | |
| 34 | + $excludeId = absint($variantIds[0]); | |
| 35 | + $skuRules[] = 'unique:fct_product_variations,sku,' . $excludeId; | |
| 36 | + } | |
| 37 | + | |
| 13 | 38 | return [ |
| 14 | 39 | 'variant_ids' => 'required|array', |
| 40 | + 'sku' => $skuRules, | |
| 15 | 41 | ]; |
| 16 | 42 | } |
| 17 | 43 | |
| 18 | 44 | public function messages() |
| @@ -19,8 +45,9 @@ | ||
| 19 | 45 | { |
| 20 | 46 | return [ |
| 21 | 47 | 'variant_ids.required' => esc_html__('At least one variant ID is required.', 'fluent-cart'), |
| 22 | 48 | 'variant_ids.array' => esc_html__('variant_ids must be an array.', 'fluent-cart'), |
| 49 | + 'sku.unique' => esc_html__('The SKU must be unique.', 'fluent-cart'), | |
| 23 | 50 | ]; |
| 24 | 51 | } |
| 25 | 52 | |
| 26 | 53 | public function sanitize() |
| @@ -34,8 +61,11 @@ | ||
| 34 | 61 | array_values(array_filter(array_map('absint', $value))), |
| 35 | 62 | 0, |
| 36 | 63 | self::MAX_VARIANTS_PER_REQUEST |
| 37 | 64 | ); |
| 65 | + }, | |
| 66 | + 'sku' => function ($value) { | |
| 67 | + return ($value !== null && $value !== '') ? sanitize_text_field($value) : null; | |
| 38 | 68 | }, |
| 39 | 69 | ]; |
| 40 | 70 | } |
| 41 | 71 | } |