Hi
I’m using Uniform for Kirby to add an Contact Form to a site. There i need the user to accept the privacy policy. If he does not he should be unable to send the form.
If i simply say ‘required’ in my rules, i can never send the form, and if i write nothing i can naturally always send the form.
How can i test if the user checked the checkbox for the privacy policy?
My Controller:
<?php
use Uniform\Form;
return function ($site, $pages, $page)
{
$form = new Form([
'email' => [
'rules' => ['required', 'email'],
'message' => 'Geben sie eine gültige E-Mail-Adresse an',
],
'telefon' => [
'rules' => [],
],
'name' => [
'rules' => ['required'],
'message' => 'Geben sie Ihren Namen an',
],
'strasse' => [
'rules' => [],
],
'ort' => [
'rules' => [],
],
'message' => [
'rules' => ['required'],
'message' => 'Hinterlassen sie eine Nachricht',
],
'datenschutz' => [
'rules' => [],
'message' => 'Sie müssen die Datenschutzbestimmungen akzeptieren',
],
]);
if (r::is('POST')) {
$form->emailAction([
'to' => 'info@example.com',
'from' => 'info@example.com',
// Dynamically generate the subject with a template.
'subject' => 'Neue Nachricht',
])
->logAction([
'file' => kirby()->roots()->site().'/registrations.log',
])
->emailAction([
// Send the success email to the email address of the submitter.
'to' => $form->data('email'),
'from' => 'info@example.com',
// Set replyTo manually, else it would be set to the value of 'email'.
'replyTo' => 'info@example.com',
'subject' => 'Kontakt KL',
// Use a snippet for the email body (see below).
'snippet' => 'emails/success',
]);
if ($form->success()) {
//go(page('registration/success')->url());
}
}
return compact('form');
};
?>
My Template:
<form class="contactform <?php if ($form->success()) echo "success"; ?>" id="form" action="<?php echo $page->url()."#form" ?>" method="POST">
<?php if ($form->success()): ?>
Vielen Dank für Ihre Nachricht!
<?php endif;?>
<hr>
<div class="contactform_line">
<label>Email*</label>
<input<?php if ($form->error('email')): ?> class="error"<?php endif; ?> name="email" type="email" value="<?php echo $form->old('email') ?>">
<?php snippet('form/error', ['field' => 'email']) ?>
</div>
<hr>
<div class="contactform_line">
<label>Name*</label>
<input<?php if ($form->error('name')): ?> class="error"<?php endif; ?> name="name" type="text" value="<?php echo $form->old('name') ?>">
<?php snippet('form/error', ['field' => 'name']) ?>
</div>
<hr>
<div class="contactform_line">
<label>Strasse</label>
<input<?php if ($form->error('strasse')): ?> class="error"<?php endif; ?> name="strasse" type="text" value="<?php echo $form->old('strasse') ?>">
<?php snippet('form/error', ['field' => 'strasse']) ?>
</div>
<hr>
<div class="contactform_line">
<label>PLZ / Ort</label>
<input<?php if ($form->error('ort')): ?> class="error"<?php endif; ?> name="ort" type="text" value="<?php echo $form->old('ort') ?>">
<?php snippet('form/error', ['field' => 'ort']) ?>
</div>
<hr>
<div class="contactform_line">
<label>Telefon</label>
<input<?php if ($form->error('telefon')): ?> class="error"<?php endif; ?> name="telefon" type="tel" value="<?php echo $form->old('tel') ?>">
<?php snippet('form/error', ['field' => 'tel']) ?>
</div>
<hr>
<div class="contactform_line">
<label>Nachricht*</label>
<textarea id="textarea" rows="3"<?php if ($form->error('message')): ?> class="error"<?php endif; ?> name="message"><?php echo $form->old('message') ?></textarea>
<?php snippet('form/error', ['field' => 'message']) ?>
</div>
<?php echo csrf_field() ?>
<?php echo honeypot_field() ?>
<hr>
<div class="contactform_datenschutz checkbox">
<input<?php if ($form->error('datenschutz')): ?> class="error"<?php endif; ?> name="datenschutz" type="checkbox" value="<?php echo $form->old('datenschutz') ?>">
<label> Ich akzeptiere die Datenschutzbestimmungen.</label>
</div>
<input type="submit" value="Senden">
<!-- Show errors of the email actions if there are any -->
<?php snippet('form/error', ['field' => \Uniform\Actions\EmailAction::class]) ?>
</form>
Thanks for your help!