Hi there,
I followed the example in the documents for a simple contact form. Upon testing the form is was failing to submit and I was getting a generic mailer error that told me nothing. I search the forum and found an old post where a suggestion was made to edit that controller to see if the form data is being captured.
For clarity I am using Kirby in Docker with Mailhog setup. I have got mailhog configured in my config.php as per the docs.
My controller:
<?php
return function($kirby, $pages, $page) {
$alert = null;
if($kirby->request()->is('POST') && get('submit')) {
dump($_POST); // check if you get into this if-statement at all
// check the honeypot
if(empty(get('website')) === false) {
go($page->url());
exit;
}
$data = [
'name' => get('name'),
'email' => get('email'),
'text' => get('text')
];
$rules = [
'name' => ['required', 'minLength' => 3],
'email' => ['required', 'email'],
'text' => ['required', 'minLength' => 3, 'maxLength' => 3000],
];
$messages = [
'name' => 'Please enter a valid name',
'email' => 'Please enter a valid email address',
'text' => 'Please enter a text between 3 and 3000 characters'
];
// some of the data is invalid
if($invalid = invalid($data, $rules, $messages)) {
$alert = $invalid;
// the data is fine, let's send the email
} else {
dump($data);
try {
$kirby->email([
//...
]);
} catch (Exception $error) {
if(option('debug')):
$alert['error'] = 'The form could not be sent: ' . $error->getMessage();
else:
$alert['error'] = 'The form could not be sent!';
endif;
}
// no exception occurred, let's send a success message
if (empty($alert) === true) {
$success = 'Your message has been sent, thank you. We will get back to you soon!';
$data = [];
}
}
}
return [
'alert' => $alert,
'data' => $data ?? false,
'success' => $success ?? false
];
};
and my contact.php
<?php if($success): ?>
<div class="alert success">
<p><?= $success ?></p>
</div>
<?php else: ?>
<?php if (isset($alert['error'])): ?>
<div><?= $alert['error'] ?></div>
<?php endif ?>
<form method="post" action="<?= $page->url() ?>">
<fieldset class="[ flow ]">
<div class="honeypot [ visually-hidden ]">
<label for="website">Website <abbr title="required">*</abbr></label>
<input type="url" id="website" name="website" tabindex="-1">
</div>
<div class="field">
<legend class="visually-hidden">Name</legend>
<label for="name">
Name <abbr title="required">*</abbr>
</label>
<input type="text" id="name" name="name" value="<?= esc($data['name'] ?? '', 'attr') ?>" required>
<?= isset($alert['name']) ? '<span class="alert error">' . esc($alert['name']) . '</span>' : '' ?>
</div>
<div class="field">
<legend class="visually-hidden">Telephone</legend>
<label for="tel">
Telephone <abbr title="required">*</abbr>
</label>
<input type="text" id="tel" name="tel" value="<?= esc($data['tel'] ?? '', 'attr') ?>" required>
<?= isset($alert['tel']) ? '<span class="alert error">' . esc($alert['tel']) . '</span>' : '' ?>
</div>
<div class="field">
<legend class="visually-hidden">Email</legend>
<label for="email">
Email <abbr title="required">*</abbr>
</label>
<input type="email" id="email" name="email" value="<?= esc($data['email'] ?? '', 'attr') ?>" required>
<?= isset($alert['email']) ? '<span class="alert error">' . esc($alert['email']) . '</span>' : '' ?>
</div>
<div class="field">
<legend class="visually-hidden">Message</legend>
<label for="text">
Message <abbr title="required">*</abbr>
</label>
<textarea id="text" name="text" rows="10" required>
<?= esc($data['text'] ?? '') ?>
</textarea>
<?= isset($alert['text']) ? '<span class="alert error">' . esc($alert['text']) . '</span>' : '' ?>
</div>
<input type="submit" name="submit" value="Submit">
</fieldset>
</form>
<?php endif ?>
At the top I can see the form field data being dumped.
Array
(
[website] =>
[name] => Mathew
[tel] => 5464565464564
[email] => myemail@gmail.com
[text] => sdf fdsfsd sd fsd fsdsdfsdf
[submit] => Submit
)
Array
(
[name] => Mathew
[email] => myemail@gmail.com
[text] => sdf fdsfsd sd fsd fsdsdfsdf
)
The error I then get in the from the form is now:
The form could not be sent: The property “body” is required