Skip to content

Commit

Permalink
feat: Validation
Browse files Browse the repository at this point in the history
Improvements
  • Loading branch information
lee-to committed Feb 2, 2025
1 parent d31d481 commit 0496c76
Showing 1 changed file with 26 additions and 3 deletions.
29 changes: 26 additions & 3 deletions src/Fields/Layouts.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit 0496c76

Please sign in to comment.