improve form submission handling and frontend assets
Replace Alpine form bindings with accessible vanilla JavaScript submission states and unique field IDs. Add the bundled frontend script and Rolldown build configuration, update the generated stylesheet, and extend submission handler tests.
This commit is contained in:
@@ -5,44 +5,27 @@ if ($id > 0):
|
||||
|
||||
$form = $formRepository->findWithId($id);
|
||||
$fields = json_decode($form["fields"], true);
|
||||
$formInstanceId = uniqid("form-$id-");
|
||||
?>
|
||||
|
||||
<div class="mx-auto w-full max-w-2xl">
|
||||
<div x-data="{data: {fields : {}}, status: 'idle', submitted: false, errorMessage: ''}" class="space-y-4">
|
||||
<form x-show="!submitted" x-transition:leave x-transition:leave-opacity-0 x-on:submit.prevent="
|
||||
status = 'busy';
|
||||
errorMessage = '';
|
||||
try {
|
||||
const response = await fetch('/ext--form/submit?id=<?= $id ?>', {
|
||||
method: 'POST',
|
||||
headers: {'Content-Type': 'application/json'},
|
||||
body: JSON.stringify(data)
|
||||
});
|
||||
const result = await response.json();
|
||||
if (!response.ok || !result.success) {
|
||||
throw new Error((result.errors || []).join(' ') || 'Your submission could not be completed. Please try again.');
|
||||
}
|
||||
submitted = true;
|
||||
} catch (error) {
|
||||
errorMessage = error.message || 'Your submission could not be completed. Please try again.';
|
||||
} finally {
|
||||
status = 'idle';
|
||||
}
|
||||
">
|
||||
<?php foreach ($fields as $field):
|
||||
<div class="space-y-4">
|
||||
<form data-form-wrapper data-form-id="<?= phpb_e($id) ?>" aria-busy="false">
|
||||
<?php foreach ($fields as $fieldIndex => $field):
|
||||
|
||||
$fieldName = $field["name"];
|
||||
$fieldLabel = $field["label"];
|
||||
$fieldType = $field["type"];
|
||||
$fieldId = $formInstanceId . "-field-" . (int) $fieldIndex;
|
||||
$fieldRequired =
|
||||
isset($field["required"]) && $field["required"];
|
||||
$fieldOptions = isset($field["options"])
|
||||
? $field["options"]
|
||||
: [];
|
||||
?>
|
||||
<div class="form-control mb-5">
|
||||
<?php if ($fieldType === "checkbox" || $fieldType === "radio"): ?>
|
||||
<fieldset class="space-y-2">
|
||||
?>
|
||||
<div class="form-control mb-5">
|
||||
<?php if ($fieldType === "checkbox" || $fieldType === "radio"): ?>
|
||||
<fieldset class="space-y-2" data-field-type="<?= phpb_e($fieldType) ?>" data-field-required="<?= $fieldRequired ? "true" : "false" ?>">
|
||||
<legend class="label mb-1 px-0">
|
||||
<span class="label-text font-medium">
|
||||
<?= phpb_e($fieldLabel) ?>
|
||||
@@ -53,7 +36,7 @@ if ($id > 0):
|
||||
</span>
|
||||
</legend>
|
||||
<?php else: ?>
|
||||
<label for="form-field-<?= phpb_e($fieldName) ?>" class="label mb-1 px-0">
|
||||
<label for="<?= phpb_e($fieldId) ?>" class="label mb-1 px-0">
|
||||
<span class="label-text font-medium">
|
||||
<?= phpb_e($fieldLabel) ?>
|
||||
<?php if ($fieldRequired): ?>
|
||||
@@ -64,7 +47,7 @@ if ($id > 0):
|
||||
</label>
|
||||
<?php endif; ?>
|
||||
<?php if ($fieldType === "select"): ?>
|
||||
<select id="form-field-<?= phpb_e($fieldName) ?>" x-model="data.fields.<?= $fieldName ?>" class="select select-bordered w-full" <?= $fieldRequired
|
||||
<select id="<?= phpb_e($fieldId) ?>" name="<?= phpb_e($fieldName) ?>" class="select select-bordered w-full" <?= $fieldRequired
|
||||
? "required"
|
||||
: "" ?>>
|
||||
<option value="" disabled selected>Please select</option>
|
||||
@@ -76,11 +59,11 @@ if ($id > 0):
|
||||
</select>
|
||||
<?php elseif ($fieldType === "checkbox"): ?>
|
||||
<div class="space-y-2">
|
||||
<?php foreach ($fieldOptions as $option): ?>
|
||||
<?php foreach ($fieldOptions as $optionIndex => $option): ?>
|
||||
<label class="label cursor-pointer justify-start gap-3 rounded-box border border-base-300 px-3 py-2">
|
||||
<input type="checkbox" value="<?= phpb_e($option[
|
||||
<input type="checkbox" name="<?= phpb_e($fieldName) ?>" value="<?= phpb_e($option[
|
||||
"value"
|
||||
]) ?>" x-model="data.fields.<?= $fieldName ?>" class="checkbox checkbox-sm checkbox-primary" />
|
||||
]) ?>" <?= $fieldRequired && $optionIndex === 0 ? "required" : "" ?> class="checkbox checkbox-sm checkbox-primary" />
|
||||
<span class="label-text"><?= phpb_e($option[
|
||||
"label"
|
||||
]) ?></span>
|
||||
@@ -89,11 +72,11 @@ if ($id > 0):
|
||||
</div>
|
||||
<?php elseif ($fieldType === "radio"): ?>
|
||||
<div class="space-y-2">
|
||||
<?php foreach ($fieldOptions as $option): ?>
|
||||
<?php foreach ($fieldOptions as $optionIndex => $option): ?>
|
||||
<label class="label cursor-pointer justify-start gap-3 rounded-box border border-base-300 px-3 py-2">
|
||||
<input type="radio" name="<?= phpb_e($fieldName) ?>" value="<?= phpb_e($option[
|
||||
<input type="radio" id="<?= phpb_e($fieldId) ?>-<?= (int) $optionIndex ?>" name="<?= phpb_e($fieldName) ?>" value="<?= phpb_e($option[
|
||||
"value"
|
||||
]) ?>" x-model="data.fields.<?= $fieldName ?>" class="radio radio-sm radio-primary" />
|
||||
]) ?>" <?= $fieldRequired && $optionIndex === 0 ? "required" : "" ?> class="radio radio-sm radio-primary" />
|
||||
<span class="label-text"><?= phpb_e($option[
|
||||
"label"
|
||||
]) ?></span>
|
||||
@@ -101,11 +84,11 @@ if ($id > 0):
|
||||
<?php endforeach; ?>
|
||||
</div>
|
||||
<?php elseif ($fieldType === "textarea"): ?>
|
||||
<textarea id="form-field-<?= phpb_e($fieldName) ?>" x-model="data.fields.<?= $fieldName ?>" class="textarea textarea-bordered w-full" placeholder="<?= phpb_e($fieldLabel) ?>" <?= $fieldRequired
|
||||
<textarea id="<?= phpb_e($fieldId) ?>" name="<?= phpb_e($fieldName) ?>" class="textarea textarea-bordered w-full" placeholder="<?= phpb_e($fieldLabel) ?>" <?= $fieldRequired
|
||||
? "required"
|
||||
: "" ?> rows="4"></textarea>
|
||||
<?php else: ?>
|
||||
<input id="form-field-<?= phpb_e($fieldName) ?>" type="<?= phpb_e($fieldType) ?>" x-model="data.fields.<?= $fieldName ?>" class="input input-bordered w-full" placeholder="<?= phpb_e($fieldLabel) ?>" <?= $fieldRequired
|
||||
<input id="<?= phpb_e($fieldId) ?>" name="<?= phpb_e($fieldName) ?>" type="<?= phpb_e($fieldType) ?>" class="input input-bordered w-full" placeholder="<?= phpb_e($fieldLabel) ?>" <?= $fieldRequired
|
||||
? "required"
|
||||
: "" ?>>
|
||||
<?php endif; ?>
|
||||
@@ -116,20 +99,20 @@ if ($id > 0):
|
||||
<?php
|
||||
endforeach; ?>
|
||||
<div class="mt-6">
|
||||
<button type="submit" class="btn btn-primary w-full" :disabled="status === 'busy'" :aria-busy="status === 'busy'">
|
||||
<span x-show="status === 'idle'" aria-live="polite">
|
||||
<button type="submit" class="btn btn-primary w-full" data-form-submit aria-busy="false">
|
||||
<span data-form-submit-idle aria-live="polite">
|
||||
<i class="ph-duotone ph-paper-plane-tilt" aria-hidden="true"></i>
|
||||
Submit
|
||||
</span>
|
||||
<span x-show="status === 'busy'" role="status" aria-live="polite">
|
||||
<span data-form-submit-busy class="hidden" hidden role="status" aria-live="polite">
|
||||
<span class="loading loading-spinner loading-sm" aria-hidden="true"></span>
|
||||
Sending...
|
||||
</span>
|
||||
</button>
|
||||
</div>
|
||||
</form>
|
||||
<div x-show="errorMessage" x-text="errorMessage" class="alert alert-error mt-4 shadow-sm" role="alert" aria-live="assertive" aria-atomic="true"></div>
|
||||
<div x-show="submitted" x-transition:enter x-transition:enter-opacity-0 x-transition:enter-delay-100 class="card mt-4 border border-success/30 bg-base-100 shadow-sm" role="status" aria-live="polite">
|
||||
<div data-form-error class="alert alert-error mt-4 shadow-sm hidden" hidden role="alert" aria-live="assertive" aria-atomic="true"></div>
|
||||
<div data-form-success class="card mt-4 border border-success/30 bg-base-100 shadow-sm hidden" hidden role="status" aria-live="polite">
|
||||
<div class="card-body items-center text-center">
|
||||
<div class="mb-2 flex h-16 w-16 items-center justify-center rounded-full bg-success/20">
|
||||
<i class="ph-duotone ph-check text-3xl text-success" aria-hidden="true"></i>
|
||||
|
||||
Reference in New Issue
Block a user