Thank you so much!
Everything is working exactly as I envisioned.
If anyone needs a form with upload, checkbox, and radio buttons, along with their valid/invalid feedback, here’s the source code.
controller:
<?php return function($kirby, $page) {
if ($kirby->request()->is('POST') && get('submit')) {
$alert = null;
$attachments = [];
$fileErrors = [];
if (empty(get('website')) === false) {go($site->url()); exit;}
$data = [
'name' => get('name'),
'nachname' => get('nachname'),
'email' => get('email'),
'phone' => get('phone'),
'bday' => get('bday'),
'geburtsort' => get('geburtsort'),
'street' => get('street'),
'hausnummer' => get('hausnummer'),
'plz' => get('plz'),
'ort' => get('Ort'),
'execute' => get('execute'),
'execute_other' => get('execute_other'),
'qualifikation' => get('qualifikation'),
'qualifikation_other' => get('qualifikation_other'),
'message' => get('message'),
];
$rules = [
'name' => ['required', 'min' => 3],
'nachname' => ['required', 'min' => 3],
'email' => ['required', 'email'],
'phone' => ['required'],
'bday' => ['required'],
'geburtsort' => [],
'street' => ['required', 'min' => 3],
'hausnummer' => ['required'],
'plz' => ['required'],
'ort' => ['required', 'min' => 3],
'execute' => ['required'],
'execute_other' => [],
'qualifikation' => ['required'],
'qualifikation_other' => [],
'message' => [],
];
$messages = [
'name' => 'Bitte geben Sie Ihren Vornamen ein.',
'nachname' => 'Bitte geben Sie Ihren Nachnamen ein.',
'email' => 'Bitte geben Sie eine gültige E-Mail Adresse ein.',
'phone' => 'Bitte geben Sie Ihre Telefonnummer ein.',
'bday' => 'Bitte geben Sie Ihr Geburtsdatum ein.',
'geburtsort' => '',
'street' => 'Bitte geben Sie Ihre Straße ein.',
'hausnummer' => 'Bitte geben Sie Ihre Hausnummer ein.',
'plz' => 'Bitte geben Sie Ihre Postleitzahl ein.',
'ort' => 'Bitte geben Sie Ihren Wohnort ein.',
'execute' => 'Bitte geben Sie Ihre bevorzugten Tätigkeiten ein.',
'execute_other' => '',
'qualifikation' => 'Bitte geben Sie Ihre Qualifikation an.',
'qualifikation_other' => '',
'message' => ''
];
if ($invalid = invalid($data, $rules, $messages)) {$alert = $invalid;}
$uploads = $kirby->request()->files()->get('file');
if (count($uploads) > 3) {$alert[] = 'Sie können maximal 3 PDFs hochladen.';}
foreach ($uploads as $upload)
{
$fileError = '';
if ($upload['error'] === 4) {$fileError = 'Sie müssen mindestens ein PDF hochladen.';}
elseif ($upload['error'] !== 0) {$fileError = 'Die Datei konnte nicht hochgeladen werden.';}
elseif ($upload['size'] > 5000000) {$fileError = $upload['name'] . ' is larger than 5 MB';}
elseif ($upload['type'] !== 'application/pdf') {$fileError = $upload['name'] . ' Ist kein PDF.';}
else {$name = $upload['tmp_name']; $tmpName = pathinfo($name); $filename = $tmpName['dirname']. '/'. F::safeName($upload['name']);
if (rename($upload['tmp_name'], $filename)) {$name = $filename;} $attachments[] = $name;
}
}
if (empty($fileError) === false) {$alert['fileError'] = $fileError;}
// if (count($fileErrors)) {$alert['fileErrors'] = $fileErrors;}
if (empty($alert)) {try
{
$kirby->email([
'template' => 'email',
'from' => 'noreply@xyz.de',
'replyTo' => $data['email'],
'to' => 'xyz@xyz.de',
'subject' => esc($data['name']) . ' hat eine Bewerbung eingereicht',
'data' => [
'name' => esc($data['name']),
'nachname' => esc($data['nachname']),
'email' => esc($data['email']),
'phone' => esc($data['phone']),
'bday' => esc($data['bday']),
'geburtsort' => esc($data['geburtsort']),
'street' => esc($data['street']),
'hausnummer' => esc($data['hausnummer']),
'plz' => esc($data['plz']),
'ort' => esc($data['ort']),
'execute' => $data['execute'],
'execute_other' => esc($data['execute_other']),
'qualifikation' => $data['qualifikation'],
'qualifikation_other' => esc($data['qualifikation_other']),
'message' => esc($data['message']),
'attachments' => $attachments
],
'attachments' => $attachments
]);
} catch (Exception $error) {
if (option('debug')):
$alert['error'] = 'The form could not be sent: <strong>' . $error->getMessage() . '</strong>';
else:
$alert['error'] = 'The form could not be sent!';
endif;
}
if (empty($alert) === true) {
$kirby->session()->set([
'name' => esc($data['name'])
]);
go('success');
}
}
}
return [
'alert' => $alert ?? null,
'data' => $data ?? false,
];
};
form:
<?php if ($alert): $anchor = "#cp_career_form";?>
<script type="text/javascript">window.location.hash = "<?= $anchor ; ?>";</script>
<div class="form_alerts f_600 p-3 p-md-4 mb-4">
<h3 class="dr f_600">Warnmeldungen</h3>
<ul class="m-0 p-0">
<?php foreach (($alert) as $message): ?><li><i class="bi bi-exclamation-diamond"></i> <?php echo $message ?></li><?php endforeach ?>
</ul>
</div>
<?php endif ?>
<form method="post" action="<?= $page->url() ?>" enctype="multipart/form-data">
<div class="row">
<div class="honeymoon col-12">
<label class="form-label f_600" for="website">Website <abbr title="required">*</abbr></label>
<input aria-describedby="website-invalid" type="website" id="website" name="website">
<div id="website-invalid" class="invalid-feedback text_sm">Honey Honey Honey</div>
</div>
<div class="col-12 col-md-6">
<label class="form-label f_600" for="name">Vorname <abbr title="required">*</abbr></label>
<input aria-describedby="name-invalid" class="form-control<?php if (isset($alert['name'])): ?> is-invalid<?php endif ?>" type="text" id="name" name="name" value="<?= $data['name'] ?? null ?>">
<div id="name-invalid" class="invalid-feedback text_sm"><?= isset($alert['name']) ? html($alert['name']) : '' ?></div>
</div>
<div class="col-12 col-md-6">
<label class="form-label f_600" for="nachname">Nachname <abbr title="required">*</abbr></label>
<input aria-describedby="nachname-invalid" class="form-control<?php if (isset($alert['nachname'])): ?> is-invalid<?php endif ?>" type="text" id="nachname" name="nachname" value="<?= $data['nachname'] ?? null ?>">
<div id="nachname-invalid" class="invalid-feedback text_sm"><?= isset($alert['nachname']) ? html($alert['nachname']) : '' ?></div>
</div>
<div class="col-12 col-md-6 mt-4">
<label class="form-label f_600" for="email">Email <abbr title="required">*</abbr></label>
<input aria-describedby="email-invalid" class="form-control<?php if (isset($alert['email'])): ?> is-invalid<?php endif ?>" type="email" id="email" name="email" value="<?= $data['email'] ?? null ?>">
<div id="email-invalid" class="invalid-feedback text_sm"><?= isset($alert['email']) ? html($alert['email']) : '' ?></div>
</div>
<div class="col-12 col-md-6 mt-4">
<label class="form-label f_600" class="form-label" for="phone">Telefonnummer: <abbr title="required">*</abbr></label>
<input aria-describedby="phone-invalid" class="form-control<?php if (isset($alert['phone'])): ?> is-invalid<?php endif ?>" type="tel" id="phone" name="phone" value="<?= $data['phone'] ?? null ?>">
<div id="phone-invalid" class="invalid-feedback text_sm"><?= isset($alert['phone']) ? html($alert['phone']) : '' ?></div>
</div>
<div class="col-12 col-md-6 mt-4">
<label class="form-label f_600" for="bday">Geburtsdatum <abbr title="required">*</abbr></label>
<input aria-describedby="bday-invalid" class="form-control<?php if (isset($alert['bday'])): ?> is-invalid<?php endif ?>" type="date" id="bday" name="bday" value="<?= $data['bday'] ?? null ?>">
<div id="bday-invalid" class="invalid-feedback text_sm"><?= isset($alert['bday']) ? html($alert['bday']) : '' ?></div>
</div>
<div class="col-12 col-md-6 mt-4">
<label class="form-label f_600" for="geburtsort">Geburtsort</label>
<input class="form-control" type="text" id="geburtsort" name="geburtsort" value="<?= $data['geburtsort'] ?? null ?>">
</div>
<div class="col-12 col-md-8 mt-4">
<label class="form-label f_600" for="street">Straße <abbr title="required">*</abbr></label>
<input aria-describedby="street-invalid" class="form-control<?php if (isset($alert['street'])): ?> is-invalid<?php endif ?>" type="text" id="street" name="street" value="<?= $data['street'] ?? null ?>">
<div id="street-invalid" class="invalid-feedback text_sm"><?= isset($alert['street']) ? html($alert['street']) : '' ?></div>
</div>
<div class="col-12 col-md-4 mt-4">
<label class="form-label f_600" for="hausnummer">Hausnummer <abbr title="required">*</abbr></label>
<input aria-describedby="hausnummer-invalid" class="form-control<?php if (isset($alert['hausnummer'])): ?> is-invalid<?php endif ?>" type="number" id="hausnummer" name="hausnummer" value="<?= $data['hausnummer'] ?? null ?>">
<div id="hausnummer-invalid" class="invalid-feedback text_sm"><?= isset($alert['hausnummer']) ? html($alert['hausnummer']) : '' ?></div>
</div>
<div class="col-12 col-md-4 mt-4">
<label class="form-label f_600" for="plz">PLZ <abbr title="required">*</abbr></label>
<input aria-describedby="plz-invalid" class="form-control<?php if (isset($alert['plz'])): ?> is-invalid<?php endif ?>" type="number" id="plz" name="plz" value="<?= $data['plz'] ?? null ?>">
<div id="plz-invalid" class="invalid-feedback text_sm"><?= isset($alert['plz']) ? html($alert['plz']) : '' ?></div>
</div>
<div class="col-12 col-md-8 mt-4">
<label class="form-label f_600" for="street">Ort <abbr title="required">*</abbr></label>
<input aria-describedby="ort-invalid" class="form-control<?php if (isset($alert['ort'])): ?> is-invalid<?php endif ?>" type="text" id="ort" name="ort" value="<?= $data['ort'] ?? null ?>">
<div id="ort-invalid" class="invalid-feedback text_sm"><?= isset($alert['ort']) ? html($alert['ort']) : '' ?></div>
</div>
<div class="col-12 col-md-8 mt-4">
<p class="f_600 mb-1 mt-1">Bevorzugte Tätigkeiten <abbr title="required">*</abbr></p>
<div class="form-check">
<input class="form-check-input<?php if (isset($alert['execute'])): ?> is-invalid<?php endif ?>" type="checkbox" id="execute1" name="execute[]" value="Pflege" <?= isset($data['execute']) && in_array('Pflege', $data['execute']) ? 'checked' : '' ?>>
<label class="form-check-label" for="execute1">Pflege</label>
</div>
<div class="form-check">
<input class="form-check-input<?php if (isset($alert['execute'])): ?> is-invalid<?php endif ?>" type="checkbox" id="execute2" name="execute[]" value="Hauswirtschaft" <?= isset($data['execute']) && in_array('Hauswirtschaft', $data['execute']) ? 'checked' : '' ?>">
<label class="form-check-label" for="execute2">Hauswirtschaft</label>
</div>
<div class="form-check">
<input aria-describedby="execute-invalid" class="form-check-input<?php if (isset($alert['execute'])): ?> is-invalid<?php endif ?>" type="checkbox" id="execute3" name="execute[]" value="Sonstiges" <?= isset($data['execute']) && in_array('Sonstiges', $data['execute']) ? 'checked' : '' ?>">
<label class="form-check-label" for="execute3">Sonstiges</label>
<!--last one get invalid feedback cause of diff area labels-->
<div id="execute-invalid" class="invalid-feedback text_sm"><?= isset($alert['execute']) ? html($alert['execute']) : '' ?></div>
</div>
</div>
<div class="col-12 mt-2 execute_other mb-2">
<textarea placeholder="Beschreiben Sie Ihre bevorzugten Tätigkeiten…" class="form-control" id="execute_other" name="execute_other"><?= $data['execute_other']?? null ?></textarea>
</div>
<div class="col-12 col-md-8 mt-4">
<p class="f_600 mb-1 mt-1">Ihre Qualifikation <abbr title="required">*</abbr></p>
<?php $value = $data['qualifikation']?? '' ?>
<div class="form-check">
<input type="radio" name="qualifikation" id="qualifikation1" value="Pflegefachkraft" class="checkquali form-check-input<?php if (isset($alert['qualifikation'])): ?> is-invalid<?php endif ?>" <?php e($value=='Pflegefachkraft', 'checked')?>>
<label class="form-check-label" for="qualifikation1">Pflegefachkraft</label>
</div>
<div class="form-check">
<input type="radio" name="qualifikation" id="qualifikation2" value="Pflegekraft_mit_einjaehriger_Ausbildung" class="checkquali form-check-input<?php if (isset($alert['qualifikation'])): ?> is-invalid<?php endif ?>" <?php e($value=='Pflegekraft_mit_einjaehriger_Ausbildung', 'checked')?>>
<label class="form-check-label" for="qualifikation2">Pflegekraft mit einjähriger Ausbildung</label>
</div>
<div class="form-check">
<input type="radio" name="qualifikation" id="qualifikation3" value="Pflegekraft_mit_Pflegebasiskurs" class="checkquali form-check-input<?php if (isset($alert['qualifikation'])): ?> is-invalid<?php endif ?>" <?php e($value=='Pflegekraft_mit_Pflegebasiskurs', 'checked')?>>
<label class="form-check-label" for="qualifikation3">Pflegekraft mit Pflegebasiskurs</label>
</div>
<div class="form-check">
<input type="radio" name="qualifikation" id="qualifikation4" value="Pflegekraft_ungelernt" class="checkquali form-check-input<?php if (isset($alert['qualifikation'])): ?> is-invalid<?php endif ?>" <?php e($value=='Pflegekraft_ungelernt', 'checked')?>>
<label class="form-check-label" for="qualifikation4">Pflegekraft ungelernt</label>
</div>
<div class="form-check qualifikation5">
<input aria-describedby="qualifikation-invalid" type="radio" name="qualifikation" id="qualifikation5" value="Andere_Qualifikation" class="checkquali form-check-input<?php if (isset($alert['qualifikation'])): ?> is-invalid<?php endif ?>" <?php e($value=='Andere_Qualifikation', 'checked')?>>
<label class="form-check-label" for="qualifikation5">Andere Qualifikation</label>
<!--last one get invalid feedback cause of diff area labels-->
<div id="qualifikation-invalid" class="invalid-feedback text_sm"><?= isset($alert['qualifikation']) ? html($alert['qualifikation']) : '' ?></div>
</div>
</div>
<div class="col-12 mt-2 qualifikation_other mb-2">
<textarea placeholder="Beschreiben Sie Ihre Qualifikationen…" class="form-control" id="qualifikation_other" name="qualifikation_other"><?= $data['qualifikation_other']?? null ?></textarea>
</div>
<div class="col-12 mt-4">
<label class="form-label f_600" for="file">Bewerbungsunterlagen hochladen <abbr title="required">*</abbr></label>
<input aria-describedby="file-invalid" class="form-control<?php if (isset($alert['fileError'])): ?> is-invalid<?php endif ?>" id="file" name="file[]" type="file" multiple>
<span class="help text_sm">Max. 3 PDF Dateien (max. Dateigröße 5MB)</span>
<div id="file-invalid" class="invalid-feedback text_sm">
<?= isset($alert['fileError']) ? html($alert['fileError']) : '' ?>
</div>
</div>
<div class="col-12 mt-4">
<label class="form-label f_600" for="message">Nachricht an uns…</label>
<textarea placeholder="Haben Sie besondere Wünsche, wie Arbeitszeit, Wochenstunden etc. …" class="form-control" id="message" name="message"><?= $data['message']?? null ?></textarea>
</div>
<div class="col-12 mt-4">
<button class="btn btn-secondary btn-lg" type="button submit" name="submit" id="submit" value="Submit">Senden</button>
</div>
</div>
</form>
Conditional fields:
<?php if ($page->intendedTemplate()->name() === 'your-template-name') : ?>
<?= js(['assets/js/conditional-field.js','@auto']) ?>
<script>
new ConditionalField({
control: '#execute3',
visibility: {
'on': '.execute_other',
}
});
new ConditionalField({
control: '.checkquali',
visibility: {
'Andere_Qualifikation': '.qualifikation_other',
}
});
</script>
<?php endif ?>
Regards
Martin