Files
plugi/extensions/plugi-ext-form/blocks/form-wrapper/view.php
T
deepDiverPaul 102fbc9464 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.
2026-11-25 15:56:26 +01:00

139 lines
7.7 KiB
PHP

<?php
$formRepository = new \Plugi\Extensions\Form\DefinitionsRepository();
$id = $block->setting("formId");
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 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" 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) ?>
<?php if ($fieldRequired): ?>
<span class="ml-1 text-error" aria-hidden="true">*</span>
<span class="sr-only">(required)</span>
<?php endif; ?>
</span>
</legend>
<?php else: ?>
<label for="<?= phpb_e($fieldId) ?>" class="label mb-1 px-0">
<span class="label-text font-medium">
<?= phpb_e($fieldLabel) ?>
<?php if ($fieldRequired): ?>
<span class="ml-1 text-error" aria-hidden="true">*</span>
<span class="sr-only">(required)</span>
<?php endif; ?>
</span>
</label>
<?php endif; ?>
<?php if ($fieldType === "select"): ?>
<select id="<?= phpb_e($fieldId) ?>" name="<?= phpb_e($fieldName) ?>" class="select select-bordered w-full" <?= $fieldRequired
? "required"
: "" ?>>
<option value="" disabled selected>Please select</option>
<?php foreach ($fieldOptions as $option): ?>
<option value="<?= phpb_e($option[
"value"
]) ?>"><?= phpb_e($option["label"]) ?></option>
<?php endforeach; ?>
</select>
<?php elseif ($fieldType === "checkbox"): ?>
<div class="space-y-2">
<?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" name="<?= phpb_e($fieldName) ?>" value="<?= phpb_e($option[
"value"
]) ?>" <?= $fieldRequired && $optionIndex === 0 ? "required" : "" ?> class="checkbox checkbox-sm checkbox-primary" />
<span class="label-text"><?= phpb_e($option[
"label"
]) ?></span>
</label>
<?php endforeach; ?>
</div>
<?php elseif ($fieldType === "radio"): ?>
<div class="space-y-2">
<?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" id="<?= phpb_e($fieldId) ?>-<?= (int) $optionIndex ?>" name="<?= phpb_e($fieldName) ?>" value="<?= phpb_e($option[
"value"
]) ?>" <?= $fieldRequired && $optionIndex === 0 ? "required" : "" ?> class="radio radio-sm radio-primary" />
<span class="label-text"><?= phpb_e($option[
"label"
]) ?></span>
</label>
<?php endforeach; ?>
</div>
<?php elseif ($fieldType === "textarea"): ?>
<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="<?= 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; ?>
<?php if ($fieldType === "checkbox" || $fieldType === "radio"): ?>
</fieldset>
<?php endif; ?>
</div>
<?php
endforeach; ?>
<div class="mt-6">
<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 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 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>
</div>
<h3 class="card-title text-lg">Thank you!</h3>
<p class="text-base-content/60">Your message has been sent successfully.</p>
</div>
</div>
</div>
</div>
<?php
else:
?>
<div class="form-wrapper mx-auto w-full max-w-2xl">
<div class="card border-2 border-dashed border-base-300 bg-base-200/50">
<div class="card-body items-center py-8 text-center">
<i class="ph-duotone ph-plus-circle text-4xl text-base-content/30" aria-hidden="true"></i>
<p class="text-sm text-base-content/50 mt-2">Select a form in the settings</p>
</div>
</div>
</div>
<?php
endif; ?>