Validate form submissions and action configuration, execute finishers with submission context, and return JSON success or error responses. Add configurable confirmation emails to submitters and report failed submissions in the form UI. Harden database and admin email finishers, and add submission handler and confirmation email checks.
139 lines
6.8 KiB
PHP
139 lines
6.8 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);
|
|
?>
|
|
|
|
<div class="max-w-2xl mx-auto">
|
|
<div x-data="{data: {fields : {}}, status: 'idle', submitted: false, errorMessage: ''}">
|
|
<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):
|
|
|
|
$fieldName = $field["name"];
|
|
$fieldLabel = $field["label"];
|
|
$fieldType = $field["type"];
|
|
$fieldRequired =
|
|
isset($field["required"]) && $field["required"];
|
|
$fieldOptions = isset($field["options"])
|
|
? $field["options"]
|
|
: [];
|
|
?>
|
|
<div class="form-control mb-4">
|
|
<label class="label mb-1">
|
|
<span class="label-text font-medium">
|
|
<?= $fieldLabel ?>
|
|
<?php if ($fieldRequired): ?>
|
|
<span class="text-error ml-1">*</span>
|
|
<?php endif; ?>
|
|
</span>
|
|
</label>
|
|
<?php if ($fieldType === "select"): ?>
|
|
<select x-model="data.fields.<?= $fieldName ?>" class="select select-bordered w-full" <?= $fieldRequired
|
|
? "required"
|
|
: "" ?>>
|
|
<option value="" disabled selected>Please select</option>
|
|
<?php foreach ($fieldOptions as $option): ?>
|
|
<option value="<?= $option[
|
|
"value"
|
|
] ?>"><?= $option["label"] ?></option>
|
|
<?php endforeach; ?>
|
|
</select>
|
|
<?php elseif ($fieldType === "checkbox"): ?>
|
|
<div class="space-y-2">
|
|
<?php foreach ($fieldOptions as $option): ?>
|
|
<label class="label cursor-pointer gap-3">
|
|
<span class="label-text"><?= $option[
|
|
"label"
|
|
] ?></span>
|
|
<input type="checkbox" value="<?= $option[
|
|
"value"
|
|
] ?>" x-model="data.fields.<?= $fieldName ?>" class="checkbox checkbox-sm checkbox-primary" />
|
|
</label>
|
|
<?php endforeach; ?>
|
|
</div>
|
|
<?php elseif ($fieldType === "radio"): ?>
|
|
<div class="space-y-2">
|
|
<?php foreach ($fieldOptions as $option): ?>
|
|
<label class="label cursor-pointer gap-3">
|
|
<span class="label-text"><?= $option[
|
|
"label"
|
|
] ?></span>
|
|
<input type="radio" name="<?= $fieldName ?>" value="<?= $option[
|
|
"value"
|
|
] ?>" x-model="data.fields.<?= $fieldName ?>" class="radio radio-sm radio-primary" />
|
|
</label>
|
|
<?php endforeach; ?>
|
|
</div>
|
|
<?php elseif ($fieldType === "textarea"): ?>
|
|
<textarea x-model="data.fields.<?= $fieldName ?>" class="textarea textarea-bordered w-full" placeholder="<?= $fieldLabel ?>" <?= $fieldRequired
|
|
? "required"
|
|
: "" ?> rows="4"></textarea>
|
|
<?php else: ?>
|
|
<input type="<?= $fieldType ?>" x-model="data.fields.<?= $fieldName ?>" class="input input-bordered w-full" placeholder="<?= $fieldLabel ?>" <?= $fieldRequired
|
|
? "required"
|
|
: "" ?>>
|
|
<?php endif; ?>
|
|
</div>
|
|
<?php
|
|
endforeach; ?>
|
|
<div class="mt-6">
|
|
<button type="submit" class="btn btn-primary w-full" :disabled="status === 'busy'">
|
|
<span x-show="status === 'idle'">
|
|
<i class="ph-duotone ph-paper-plane-tilt"></i>
|
|
Submit
|
|
</span>
|
|
<span x-show="status === 'busy'">
|
|
<span class="loading loading-spinner loading-sm"></span>
|
|
Sending...
|
|
</span>
|
|
</button>
|
|
</div>
|
|
</form>
|
|
<div x-show="errorMessage" x-text="errorMessage" class="alert alert-error mt-4" role="alert"></div>
|
|
<div x-show="submitted" x-transition:enter x-transition:enter-opacity-0 x-transition:enter-delay-100 class="card bg-base-100 shadow-lg mt-4">
|
|
<div class="card-body items-center text-center">
|
|
<div class="w-16 h-16 rounded-full bg-success/20 flex items-center justify-center mb-2">
|
|
<i class="ph-duotone ph-check text-3xl text-success"></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 max-w-2xl mx-auto">
|
|
<div class="card bg-base-200 border-2 border-dashed border-base-300">
|
|
<div class="card-body items-center text-center py-8">
|
|
<i class="ph-duotone ph-plus-circle text-4xl text-base-content/30"></i>
|
|
<p class="text-sm text-base-content/50 mt-2">Select a form in the settings</p>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
<?php
|
|
endif; ?>
|