Ah, thanks, I thought it must be something like that. Renaming the controller file to home.php solved this problem. Now I just don’t understand why I get the error
The form could not be sent: Undefined variable: text
when sending the form? I don’t have any variable like that declared anywhere. This is the form part of the template snippet:
<?php if($success): ?>
<div class="alert success">
<p><?= $success ?></p>
</div>
<?php else: ?>
<?php if (isset($alert['error'])): ?>
<div class="error"><?= $alert['error'] ?></div>
<?php endif ?>
<form method="post" action="<?= $page->url() ?>">
<div class="website">
<label for="k_website">Website <abbr title="required">*</abbr></label>
<input type="website" id="k_website" name="k_website" tabindex="-1">
</div>
<div>
<label for="k_name">Name <small>(optional)</small></label>
<input type="text" id="k_name" name="k_name" value="<?= $form_data['name'] ?? '' ?>">
<?= isset($alert['name']) ? '<span class="alert error">' . html($alert['name']) . '</span>' : '' ?>
</div>
<div>
<label for="k_email">E-Mail</label>
<input type="email" id="k_email" name="k_email" value="<?= $form_data['email'] ?? '' ?>" required>
<?= isset($alert['email']) ? '<span class="alert error">' . html($alert['email']) . '</span>' : '' ?>
</div>
<div>
<label for="k_betreff">Betreff <small>(optional)</small></label>
<input type="text" id="k_betreff" name="k_betreff" value="<?= $form_data['subject']?? '' ?>">
<?= isset($alert['subject']) ? '<span class="alert error">' . html($alert['subject']) . '</span>' : '' ?>
</div>
<div>
<label for="k_nachricht">Nachricht</label>
<textarea id="k_nachricht" name="k_nachricht" required><?= $form_data['message']?? '' ?></textarea>
<?= isset($alert['message']) ? '<span class="alert error">' . html($alert['message']) . '</span>' : '' ?>
</div>
<input type="submit" name="k_submit" value="Absenden">
</form>
<?php endif ?>
and this is the controller:
<?php
return function($kirby, $pages, $page) {
$alert = null;
if($kirby->request()->is('POST') && get('k_submit')) {
// check the honeypot
if(empty(get('k_website')) === false) {
go($page->url());
exit;
}
$form_data = [
'name' => get('k_name'),
'email' => get('k_email'),
'subject' => get('k_betreff'),
'message' => get('k_nachricht')
];
$rules = [
'email' => ['required', 'email'],
'message' => ['required', 'minLength' => 3, 'maxLength' => 3000],
];
$messages = [
'email' => 'Bitte geben sie eine gültige E-Mail-Adresse ein',
'message' => 'Der Text muss mindestens drei und darf höchstens 3000 Zeichen lang sein'
];
// some of the data is invalid
if($invalid = invalid($form_data, $rules, $messages)) {
$alert = $invalid;
// the data is fine, let's send the email
} else {
try {
$kirby->email([
'template' => 'email',
'from' => 'test@example.com',
'replyTo' => $form_data['email'],
'to' => 'test@example.com',
'subject' => empty($form_data['subject']) ? 'Nachricht über das Kontaktformular' : esc($form_data['subject']),
'data' => [
'subject' => esc($form_data['subject']),
'message' => esc($form_data['message']),
'sender' => esc($form_data['name'])
]
]);
} 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;
}
// no exception occurred, let's send a success message
if (empty($alert) === true) {
$success = 'Vielen Dank, die Nachricht wurde gesendet. Wir werden uns so bald wie möglich bei ihnen melden.';
$form_data = [];
}
}
}
return [
'alert' => $alert,
'data' => $form_data ?? false,
'success' => $success ?? false
];
};
Anyone see what the problem could be?