Uniform for Kirby require checkbox to be checked

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!

Im not sure i understand. It needs to be required to prevent submission. That is the test!

that is my understandig as well.
but when do make it required i can not submit at all: even if i check the checkbox…

I have such a rule on my site for GDPR that I know works… maybe comparing it helps…

The controller has this in it:

'gdprconsent' => [
        'rules' => ['required'],
        'message' => "Your consent is required.",
      ],

And the HTML looks like…

<div class="block-1 consent-box <?php if ($form->success()): ?>completed<?php endif; ?>">
<div class="block-col">
<h3>Important</h3>
<p>To comply with new data protection laws (<a href="https://ico.org.uk/for-organisations/guide-to-the-general-data-protection-regulation-gdpr/" target="_blank">GDPR</a>), we require your consent to record and retain your contact details. How this information is used and stored is explained within our <a href="<?= $site->file('hashandsalt-privacy-policy.pdf')->url() ?>">Privacy Policy</a>.</p>
<div class="fancy-checks">
<input <?php if ($form->error('gdprconsent')): ?> class="error"<?php endif; ?> type='checkbox' name='gdprconsent' value='consent' id="gdprconsent" />
<label for="gdprconsent">I consent to my contact details being recorded and retained.</label>
<?php if ($form->error('gdprconsent')): ?>
<p class="form-error-hint"><?= implode('<br>', $form->error('gdprconsent')) ?></p>
<?php endif; ?>
</div>
</div>
</div>

I think you might be using an old version of uniform… your syntax looks a bit odd. I am not sure this is right…

<?php snippet('form/error', ['field' => \Uniform\Actions\EmailAction::class]) ?>

Did you follow the Uniform documentation or do something special?

1 Like

thanks! i will have a close look at your example tomorrow!
most of the code i copyed from the “extended tutorial” from the documentation here: http://kirby-uniform.readthedocs.io/en/latest/examples/extended/

You should upgrade and follow this page instead (I think)…

https://kirby-uniform.readthedocs.io/en/v3.4.0/examples/extended/

Although latest should mean latest :slight_smile: Looks like the latest version is 3.4.0.

ah i figuered it out!
i copypasted the input field and only changed the type but left
<input ... type="checkbox" value="<?php echo $form->old('datenschutz') ?>">
it should be
<input ... type="checkbox" value="consent">

1 Like