From 0496c76df4da899e8178df56007cebb9bee4333a Mon Sep 17 00:00:00 2001 From: LT Date: Sun, 2 Feb 2025 15:22:04 +0300 Subject: [PATCH] feat: Validation Improvements --- src/Fields/Layouts.php | 29 ++++++++++++++++++++++++++--- 1 file changed, 26 insertions(+), 3 deletions(-) diff --git a/src/Fields/Layouts.php b/src/Fields/Layouts.php index 27c9feb..9e5bcbc 100644 --- a/src/Fields/Layouts.php +++ b/src/Fields/Layouts.php @@ -384,11 +384,34 @@ protected function resolveBeforeApply(mixed $data): mixed $column = $field?->getLabel() ?? $fieldName; $rules["$layoutName.*.$fieldName"] = $args; - $attributes["$layoutName.*.$fieldName"] = "{$layout->title()}(:position) {$column}"; + $attributes["$layoutName.*.$fieldName"] = $column; } } - - Validator::validate($value->toArray(), $rules, attributes: $attributes); + + $validator = Validator::make($value->toArray(), $rules, attributes: $attributes); + + if($validator->fails()) { + $errors = []; + + $before = array_key_first($validator->errors()->toArray()); + $beforeKeys = explode('.', $before); + $index = 1; + + foreach ($validator->errors()->toArray() as $key => $error) { + $keys = explode('.', $key); + + if($beforeKeys[0] !== $keys[0] || $beforeKeys[1] !== $keys[1]) { + $index++; + } + + $column = $keys[2] ?? ''; + + $errors["data.$index.$column"] = $error; + $beforeKeys = $keys; + } + + throw ValidationException::withMessages($errors)->errorBag($this->getFormName()); + } } return $this->resolveCallback($data, function (Field $field, mixed $value): void {