Skip to content

A free open source IT asset/license management system

split rules for import validation to custom label request

Godfrey M1a4901a6ae20
diff --git a/app/Http/Controllers/LabelsController.php b/app/Http/Controllers/LabelsController.php
index 6e60b04ea8..442052626a 100755
--- a/app/Http/Controllers/LabelsController.php
+++ b/app/Http/Controllers/LabelsController.php
@@ -260,6 +260,12 @@ class LabelsController extends Controller
public function store(CustomLabelRequest $request)
{
+ logger()->debug('Custom label validation rules', [
+ 'type' => $request->input('type'),
+ 'alignment_rule' => $request->rules()['content.barcode2D_h_align'] ?? null,
+ 'submitted_alignment' => $request->input('content.barcode2D_h_align'),
+ 'validated_alignment' => data_get($request->validated(), 'content.barcode2D_h_align'),
+ ]);
$validated = $request->validated();
$type = $validated['type'];
@@ -284,7 +290,7 @@ class LabelsController extends Controller
};
$content = $castNumeric($validated['content']);
-
+
$template = $validated['template'] ?? null;
if ($type === 'tape' && $template === 'StandardTape') {
diff --git a/app/Http/Requests/CustomLabelRequest.php b/app/Http/Requests/CustomLabelRequest.php
index af51c39ce0..f8c20b66b7 100644
--- a/app/Http/Requests/CustomLabelRequest.php
+++ b/app/Http/Requests/CustomLabelRequest.php
@@ -4,9 +4,11 @@ namespace App\Http\Requests;
use App\Models\Labels\CustomLabelFonts;
use App\Rules\LabelGeometryRules;
+use App\Rules\CustomLabelContentRules;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Validation\Rule;
-
+use Illuminate\Contracts\Validation\Validator;
+use Illuminate\Validation\ValidationException;
class CustomLabelRequest extends FormRequest
{
public function authorize(): bool
@@ -16,7 +18,14 @@ class CustomLabelRequest extends FormRequest
public function rules(): array
{
- return $this->commonRules() + ($this->input('type') === 'tape' ? $this->tapeRules() : $this->sheetRules());
+ $isTape = $this->input('type') === 'tape';
+
+ return array_merge(
+ $this->commonRules(),
+ CustomLabelContentRules::shared(),
+ $isTape ? CustomLabelContentRules::tape() : [],
+ $isTape ? $this->tapeRules() : $this->sheetRules(),
+ );
}
private function commonRules(): array
@@ -31,6 +40,7 @@ class CustomLabelRequest extends FormRequest
'content.title_font' => ['nullable', 'string', Rule::in(CustomLabelFonts::ALLOWED)],
'content.field_label_font' => ['nullable', 'string', Rule::in(CustomLabelFonts::ALLOWED)],
'content.field_value_font' => ['nullable', 'string', Rule::in(CustomLabelFonts::ALLOWED)],
+ 'content.title_offset_x' => ['required', 'numeric'],
];
}
@@ -52,4 +62,15 @@ class CustomLabelRequest extends FormRequest
'label' => ['required', 'array'],
] + LabelGeometryRules::sheet();
}
+
+ protected function failedValidation(Validator $validator)
+ {
+ logger()->debug('Custom label validation failed', [
+ 'errors' => $validator->errors()->toArray(),
+ 'type' => $this->input('type'),
+ 'submitted_alignment' => $this->input('content.barcode2D_h_align'),
+ ]);
+
+ throw new ValidationException($validator);
+ }
}
\ No newline at end of file
diff --git a/app/Rules/CustomLabelContentRules.php b/app/Rules/CustomLabelContentRules.php
new file mode 100644
index 0000000000..1b6eaa0caa
--- /dev/null
+++ b/app/Rules/CustomLabelContentRules.php
@@ -0,0 +1,72 @@
+<?php
+
+namespace App\Rules;
+
+use App\Models\Labels\CustomLabelFonts;
+use Illuminate\Validation\Rule;
+
+class CustomLabelContentRules
+{
+ public static function shared(): array
+ {
+ return [
+ 'content.barcode_size' => ['required', 'numeric'],
+ 'content.barcode_margin' => ['required', 'numeric'],
+
+ 'content.barcode_2d_size' => ['required', 'numeric'],
+ 'content.barcode2D_h_align' => ['required', 'string', 'in:L,C,R'],
+ 'content.barcode2D_v_align' => ['required', 'string', 'in:T,C,B'],
+
+ 'content.tag_alignment' => ['required', 'string', 'in:L,C,R'],
+
+ 'content.logo_max_width' => ['sometimes', 'numeric'],
+ 'content.logo_margin' => ['sometimes', 'numeric'],
+ 'content.logo_h_align' => ['sometimes', 'string', 'in:L,C,R'],
+ 'content.logo_v_align' => ['sometimes', 'string', 'in:T,C,B'],
+
+ 'content.tag_font_size' => ['required', 'numeric'],
+ 'content.tag_offset_x' => ['required', 'numeric'],
+ 'content.tag_offset_y' => ['required', 'numeric'],
+
+ 'content.title_font_size' => ['required', 'numeric'],
+ 'content.title_margin' => ['required', 'numeric'],
+
+ 'content.field_label_font_size' => ['required', 'numeric'],
+ 'content.field_label_margin' => ['required', 'numeric'],
+ 'content.field_value_font_size' => ['required', 'numeric'],
+ 'content.field_value_margin' => ['required', 'numeric'],
+
+ 'content.tag_font' => ['nullable', 'string', Rule::in(CustomLabelFonts::ALLOWED)],
+ 'content.title_font' => ['nullable', 'string', Rule::in(CustomLabelFonts::ALLOWED)],
+ 'content.field_label_font' => ['nullable', 'string', Rule::in(CustomLabelFonts::ALLOWED)],
+ 'content.field_value_font' => ['nullable', 'string', Rule::in(CustomLabelFonts::ALLOWED)],
+ ];
+ }
+
+ public static function tape(): array
+ {
+ return [
+ 'dimensions' => ['required', 'array'],
+ 'dimensions.width' => ['required', 'numeric', 'gt:0'],
+ 'dimensions.height' => ['required', 'numeric', 'gt:0'],
+ 'dimensions.label_gap' => ['nullable', 'numeric', 'min:0'],
+
+ 'content.barcode1D_v_align' => ['nullable', 'string', 'in:T,M,B'],
+ 'content.barcode1D_placement' => ['nullable', 'string', 'in:full_width,text_column,inline'],
+
+ 'content.barcode2D_placement' => ['nullable', 'string', 'in:stacked,inline,text_column'],
+ 'content.barcode_2d_margin' => ['nullable', 'numeric'],
+
+ 'content.text_size_mod' => ['nullable', 'numeric'],
+ 'content.text_area_offset_y' => ['nullable', 'numeric'],
+
+ 'content.logo_max_height' => ['nullable', 'numeric'],
+ 'content.logo_placement' => ['nullable', 'string', 'in:inline,text_column'],
+
+ 'content.tag_position_mode' => ['nullable', 'string', 'in:free,inline'],
+ 'content.field_alignment' => ['nullable', 'string', 'in:L,C,R'],
+
+ 'content.text_render_mode' => ['nullable', 'string', 'in:inline,block,vertical_stack'],
+ ];
+ }
+}
\ No newline at end of file
diff --git a/app/Services/CustomLabelImportValidator.php b/app/Services/CustomLabelImportValidator.php
index 5c1ad03673..e14c2fdab1 100644
--- a/app/Services/CustomLabelImportValidator.php
+++ b/app/Services/CustomLabelImportValidator.php
@@ -4,9 +4,8 @@ namespace App\Services;
use App\Models\Labels\CustomLabelFonts;
use App\Rules\LabelGeometryRules;
-use Illuminate\Validation\Rule;
use Illuminate\Validation\ValidationException;
-
+use App\Rules\CustomLabelContentRules;
class CustomLabelImportValidator
{
public function validate(?string $rawJson): array
@@ -29,15 +28,16 @@ class CustomLabelImportValidator
return match ($config['type'] ?? null) {
'sheet' => array_merge(
$this->baseRules(),
- $this->sharedContentRules(),
+ CustomLabelContentRules::shared(),
$this->sheetRules(),
),
'tape' => array_merge(
$this->baseRules(),
- $this->sharedContentRules(),
+ CustomLabelContentRules::shared(),
+ CustomLabelContentRules::tape(),
$this->printableAreaRules(),
- $this->tapeContentRules(),
+ $this->tapeRules(),
),
default => $this->baseRules(),
@@ -75,70 +75,6 @@ class CustomLabelImportValidator
];
}
- protected function sharedContentRules(): array
- {
- return [
- 'content.barcode_size' => ['required', 'numeric'],
- 'content.barcode_margin' => ['required', 'numeric'],
-
- 'content.barcode_2d_size' => ['required', 'numeric'],
- 'content.barcode2D_h_align' => ['required', 'string', 'in:L,C,R'],
- 'content.barcode2D_v_align' => ['required', 'string', 'in:T,C,B'],
-
- 'content.tag_alignment' => ['required', 'string', 'in:L,C,R'],
-
- 'content.logo_max_width' => ['required', 'numeric'],
- 'content.logo_margin' => ['required', 'numeric'],
- 'content.logo_h_align' => ['required', 'string', 'in:L,C,R'],
- 'content.logo_v_align' => ['required', 'string', 'in:T,C,B'],
-
- 'content.tag_font_size' => ['required', 'numeric'],
- 'content.tag_offset_x' => ['required', 'numeric'],
- 'content.tag_offset_y' => ['required', 'numeric'],
-
- 'content.title_font_size' => ['required', 'numeric'],
- 'content.title_margin' => ['required', 'numeric'],
-
- 'content.field_label_font_size' => ['required', 'numeric'],
- 'content.field_label_margin' => ['required', 'numeric'],
- 'content.field_value_font_size' => ['required', 'numeric'],
- 'content.field_value_margin' => ['required', 'numeric'],
-
- 'content.tag_font' => ['nullable', 'string', Rule::in(CustomLabelFonts::ALLOWED)],
- 'content.title_font' => ['nullable', 'string', Rule::in(CustomLabelFonts::ALLOWED)],
- 'content.field_label_font' => ['nullable', 'string', Rule::in(CustomLabelFonts::ALLOWED)],
- 'content.field_value_font' => ['nullable', 'string', Rule::in(CustomLabelFonts::ALLOWED)],
- ];
- }
-
- protected function tapeContentRules(): array
- {
- return [
- 'printable_area' => ['required', 'array'],
-
- 'dimensions' => ['required', 'array'],
- 'dimensions.width' => ['required', 'numeric', 'gt:0'],
- 'dimensions.height' => ['required', 'numeric', 'gt:0'],
- 'dimensions.label_gap' => ['nullable', 'numeric', 'min:0'],
-
- 'content.barcode1D_v_align' => ['nullable', 'string', 'in:T,M,B'],
- 'content.barcode1D_placement' => ['nullable', 'string', 'in:full_width,text_column,inline'],
-
- 'content.barcode2D_placement' => ['nullable', 'string', 'in:stacked,inline,text_column'],
- 'content.barcode_2d_margin' => ['nullable', 'numeric'],
-
- 'content.text_size_mod' => ['nullable', 'numeric'],
- 'content.text_area_offset_y' => ['nullable', 'numeric'],
-
- 'content.logo_max_height' => ['nullable', 'numeric'],
- 'content.logo_placement' => ['nullable', 'string', 'in:inline,text_column'],
-
- 'content.tag_position_mode' => ['nullable', 'string', 'in:free,inline'],
- 'content.field_alignment' => ['nullable', 'string', 'in:L,C,R'],
-
- 'content.text_render_mode' => ['nullable', 'string', 'in:inline,block,vertical_stack'],
- ];
- }
protected function sheetRules(): array
{
@@ -196,6 +132,16 @@ class CustomLabelImportValidator
);
}
+ protected function tapeRules(): array
+ {
+ return [
+ 'dimensions' => ['required', 'array'],
+ 'dimensions.width' => ['required', 'numeric', 'gt:0'],
+ 'dimensions.height' => ['required', 'numeric', 'gt:0'],
+ 'dimensions.label_gap' => ['nullable', 'numeric', 'min:0'],
+ ];
+ }
+
public function normalizeFonts(array $config): array
{
foreach ([