Hello ,
I have a problem with the radio buttons in the contact form.
The form doesn’t submit and I don’t get an error message.
Everything works without radio buttons.
When I fill out the form without selecting the radio button I get the following error message:
esc(): Argument #1 ($string) must be of type string, null given, called in on line 51
Line 51 in controller:
try {
$kirby->email([
'template' => 'email',
'from' => 'yourcontactform@yourcompany.com',
'replyTo' => $data['email'],
'to' => 'you@yourcompany.com',
'subject' => esc($data['name']) . ' sent you a message from your contact form',
'data' => [
...
/* line 51 */ 'abschluss' => esc($data['abschluss']),
...
]
]);
Controller
<?php
return function($kirby, $pages, $page) {
$alert = null;
if($kirby->request()->is('POST') && get('submit')) {
// check the honeypot
if(empty(get('website')) === false) {
go($page->url());
exit;
}
$data = [
'name' => get('name'),
'abschluss' => get('abschluss'),
'telefon' => get('telefon'),
'email' => get('email'),
'text' => get('text')
];
$rules = [
'name' => ['required', 'minLength' => 3],
'email' => ['required', 'email'],
'telefon' => ['required', 'minLength' => 5],
'abschluss' => ['in' => [['ja','nein']]],
];
$messages = [
'name' => 'Bitte tragen Sie hier Ihren Namen ein!',
'email' => 'Bitte tragen Sie hier eine gültige E-Mailadresse ein!',
'telefon' => 'Bitte tragen Sie hier Ihre Telefonnummer ein!',
];
// some of the data is invalid
if($invalid = invalid($data, $rules, $messages)) {
$alert = $invalid;
// the data is fine, let's send the email
} else {
try {
$kirby->email([
'template' => 'email',
'from' => 'yourcontactform@yourcompany.com',
'replyTo' => $data['email'],
'to' => 'you@yourcompany.com',
'subject' => esc($data['name']) . ' sent you a message from your contact form',
'data' => [
'name' => esc($data['name']),
'abschluss' => esc($data['abschluss']),
'email' => esc($data['email']),
'telefon' => esc($data['telefon']),
'text' => esc($data['text']),
]
]);
} 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 = 'Your message has been sent, thank you. We will get back to you soon!';
$data = [];
}
}
}
return [
'alert' => $alert,
'data' => $data ?? false,
'success' => $success ?? false
];
};
Form
...
<div class="mb-24">
<label for="abschluss">Mit Berufsabschluss? <abbr title="required">*</abbr></label>
<p>
<?php $value = $data['abschluss']?? '' ?>
<input type="radio" name="abschluss" value="ja" <?php e($value=='ja', 'checked')?>>Ja</input>
<input type="radio" name="abschluss" value="nein" <?php e($value=='nein', 'checked')?>>Nein</input>
<?= isset($alert['abschluss']) ? '<span class="alert error">' . html($alert['abschluss']) . '</span>' : '' ?>
</p>
</div>
...
E-Mail-Template
<p>Name: <?= $name ?></p>
<p>Abschluss: <?= $abschluss ?></p>
<p>Telefon: <?= $telefon ?></p>
<p>E-Mail: <?= $email ?></p>
<p><?= $text ?></p>
Thank you for your help.