Skip to content

Commit

Permalink
Handle edits within frontend fields (#7178)
Browse files Browse the repository at this point in the history
  • Loading branch information
jasonvarga authored Dec 8, 2022
1 parent 69f9016 commit 620a37a
Show file tree
Hide file tree
Showing 9 changed files with 326 additions and 18 deletions.
9 changes: 4 additions & 5 deletions resources/views/extend/forms/fields/checkboxes.antlers.html
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
{{ foreach:options as="value|label" }}
{{ foreach:options as="option|label" }}
<label>
<input
type="checkbox"
name="{{ handle }}[]"
value="{{ value }}"
value="{{ option }}"
{{ if js_driver }}{{ js_attributes }}{{ /if }}
{{ if (old | in_array:value) }}checked{{ /if }}
{{ if ! old && value === default }}checked{{ /if }}
{{ if value|in_array:option }}checked{{ /if }}
{{ if validate|contains:required }}required{{ /if }}
>
{{ label !== null ? label : value }}
{{ label !== null ? label : option }}
</label>
{{ unless inline }}
<br>
Expand Down
4 changes: 2 additions & 2 deletions resources/views/extend/forms/fields/default.antlers.html
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<input
type="text"
type="{{ input_type ?? 'text' }}"
name="{{ handle }}"
value="{{ old ?? default}}"
value="{{ value }}"
{{ if placeholder }}placeholder="{{ placeholder }}"{{ /if }}
{{ if character_limit }}maxlength="{{ character_limit }}"{{ /if }}
{{ if js_driver }}{{ js_attributes }}{{ /if }}
Expand Down
9 changes: 4 additions & 5 deletions resources/views/extend/forms/fields/radio.antlers.html
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
{{ foreach:options as="value|label" }}
{{ foreach:options as="option|label" }}
<label>
<input
type="radio"
name="{{ handle }}"
value="{{ value }}"
value="{{ option }}"
{{ if js_driver }}{{ js_attributes }}{{ /if }}
{{ if old === value }}checked{{ /if }}
{{ if ! old && value === default }}checked{{ /if }}
{{ if value == option }}checked{{ /if }}
{{ if validate|contains:required }}required{{ /if }}
>
{{ label !== null ? label : value }}
{{ label !== null ? label : option }}
</label>
{{ unless inline }}
<br>
Expand Down
6 changes: 3 additions & 3 deletions resources/views/extend/forms/fields/select.antlers.html
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@
{{ /if }}
</option>
{{ /unless }}
{{ foreach:options as="value|label" }}
<option value="{{ value }}"{{ if old|in_array:value or old === value }} selected{{ /if }}{{ if ! old && default === value }} selected{{ /if }}>
{{ label !== null ? label : value }}
{{ foreach:options as="option|label" }}
<option value="{{ option }}"{{ if multiple }}{{ if value|in_array:option }} selected{{ /if }}{{ else }}{{ if option == value }} selected{{ /if }}{{ /if }}>
{{ label !== null ? label : option }}
</option>
{{ /foreach:options }}
</select>
2 changes: 1 addition & 1 deletion resources/views/extend/forms/fields/text.antlers.html
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<input
type="{{ input_type ?? 'text' }}"
name="{{ handle }}"
value="{{ old ?? default }}"
value="{{ value }}"
{{ if placeholder }}placeholder="{{ placeholder }}"{{ /if }}
{{ if character_limit }}maxlength="{{ character_limit }}"{{ /if }}
{{ if js_driver }}{{ js_attributes }}{{ /if }}
Expand Down
2 changes: 1 addition & 1 deletion resources/views/extend/forms/fields/textarea.antlers.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@
{{ if js_driver }}{{ js_attributes }}{{ /if }}
{{ if validate|contains:required }}required{{ /if }}
>
{{ old ?? default }}
{{ value }}
</textarea>
2 changes: 1 addition & 1 deletion resources/views/extend/forms/fields/toggle.antlers.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
name="{{ handle }}"
value="1"
{{ if js_driver }}{{ js_attributes }}{{ /if }}
{{ if ! old && default }}checked{{ /if }}
{{ if value && value !== '0' }}checked{{ /if }}
{{ if validate|contains:required }}required{{ /if }}
>
{{ if inline_label }}
Expand Down
6 changes: 6 additions & 0 deletions src/Tags/Concerns/RendersForms.php
Original file line number Diff line number Diff line change
Expand Up @@ -127,9 +127,15 @@ protected function getRenderableField($field, $errorBag = 'default', $manipulate
{
$errors = session('errors') ? session('errors')->getBag($errorBag) : new MessageBag;

$missing = str_random();
$old = old($field->handle(), $missing);
$default = $field->value() ?? $field->defaultValue();
$value = $old === $missing ? $default : $old;

$data = array_merge($field->toArray(), [
'error' => $errors->first($field->handle()) ?: null,
'old' => old($field->handle()),
'value' => $value,
]);

if ($manipulateDataCallback instanceof Closure) {
Expand Down
Loading

0 comments on commit 620a37a

Please sign in to comment.