Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[5.x] Add frontend forms support for Group fieldtype #10976

Draft
wants to merge 4 commits into
base: 5.x
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions resources/views/extend/forms/fields/group.antlers.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<div {{ if js_driver }}{{ js_attributes }}{{ /if }}>
{{ fields }}
<div class="pb-2">
<label>
{{ display }}
{{ if validate | contains:required }}
<sup class="text-red">*</sup>
{{ /if }}
</label>
<div class="p-1">{{ field }}</div>
{{ if error }}
<p class="text-gray-500">{{ error }}</p>
{{ /if }}
</div>
{{ /fields }}
</div>
3 changes: 2 additions & 1 deletion resources/views/extend/forms/fields/text.antlers.html
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<!-- TODO: update rest of fields to use name -->
<input
type="{{ input_type ?? 'text' }}"
name="{{ handle }}"
name="{{ name }}"
value="{{ value }}"
{{ if placeholder }}placeholder="{{ placeholder }}"{{ /if }}
{{ if character_limit }}maxlength="{{ character_limit }}"{{ /if }}
Expand Down
23 changes: 22 additions & 1 deletion src/Tags/Concerns/RendersForms.php
Original file line number Diff line number Diff line change
Expand Up @@ -142,13 +142,23 @@ protected function getRenderableField($field, $errorBag = 'default', $manipulate
->filter()->all();

$data = array_merge($configDefaults, $field->toArray(), [
'handle' => $field->handle(),
'name' => $this->convertDottedHandleToInputName($field->handle()),
'instructions' => $field->instructions(),
'error' => $errors->first($field->handle()) ?: null,
'default' => $field->value() ?? $field->defaultValue(),
'old' => old($field->handle()),
'old' => old($field->handle()), // TODO: Ensure dotted path for old input works here.
'value' => $value,
], $field->fieldtype()->extraRenderableFieldData());

if ($field->fieldtype()->handle() === 'group') {
$data['fields'] = collect($field->fieldtype()->fields()->all())
->map(fn ($child) => $child->setHandle($field->handle().'.'.$child->handle()))
->map(fn ($child) => $this->getRenderableField($child, $errorBag, $manipulateDataCallback))
->values()
->all();
}

if ($manipulateDataCallback instanceof Closure) {
$data = $manipulateDataCallback($data, $field);
}
Expand All @@ -158,6 +168,17 @@ protected function getRenderableField($field, $errorBag = 'default', $manipulate
return $data;
}

protected function convertDottedHandleToInputName(string $handle): string
{
$parts = collect(explode('.', $handle));

$first = $parts->pull(0);

return $first.$parts
->map(fn ($part) => '['.$part.']')
->join('');
}

/**
* Minify field html.
*
Expand Down
Loading