Skip to content

Commit

Permalink
Fixing nested validation
Browse files Browse the repository at this point in the history
  • Loading branch information
godismyjudge95 committed Nov 22, 2023
1 parent 3088e30 commit 16716f5
Showing 1 changed file with 42 additions and 2 deletions.
44 changes: 42 additions & 2 deletions src/Fieldtypes/Group.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,16 +54,44 @@ public function fields()
return new Fields($this->config('fields'), $this->field()->parent(), $this->field());
}

public function rules(): array
{
return ['array'];
}

public function extraRules(): array
{
$rules = $this
->fields()
->addValues((array) $this->field->value())
->validator()
->withContext([
'prefix' => $this->field->handle() . '.',
])
->rules();

return collect($rules)->mapWithKeys(function ($rules, $handle) {
return [$this->field->handle() . '.' . $handle => $rules];
})->all();
}

public function extraValidationAttributes(): array
{
return collect($this->fields()->validator()->attributes())->mapWithKeys(function ($attribute, $handle) {
return [$this->field->handle() . '.' . $handle => $attribute];
})->all();
}

public function preload()
{
return $this->fields()->addValues((array) ($this->field->value() ?? $this->defaultGroupData()))->meta()->toArray();
return $this->fields()->addValues($this->field->value() ?? $this->defaultGroupData())->meta()->toArray();
}

protected function defaultGroupData()
{
return $this->fields()->all()->map(function ($field) {
return $field->fieldtype()->preProcess($field->defaultValue());
});
})->all();
}

public function augment($value)
Expand All @@ -82,4 +110,16 @@ private function performAugmentation($value, $shallow)

return $this->fields()->addValues($value ?? [])->{$method}()->values()->all();
}

public function preProcessValidatable($value)
{
return array_merge(
$value ?? [],
$this->fields()
->addValues($value ?? [])
->preProcessValidatables()
->values()
->all(),
);
}
}

0 comments on commit 16716f5

Please sign in to comment.