Uniform plugin - Keep receive_copy checkbox checked on error

With Uniform, we have to put <?= $form->old('field-name') ?> for the value attribute of fields to keep values if there an error when the form is submitted.
But what about the receive_copy checkbox ? How to keep it checked on error if the user chose to receive a copy ?


Please provide your code or a link to the form you are referring to.

The form is simple as the Uniform doc demonstrate it. 2 text inputs, 1 textarea and the checkbox.

Here the template :

<form class="form contact-form" method="post" action="<?= "{$page->url()}#envoyer-un-message" ?>" aria-label="Formulaire de contact">

    snippet('form/form-message-errors', ['form' => $form]);

<p class="fields-group">
    <label for="name">Votre nom :</label>
    <input id="name" class="field <?= ecco($form->error('name'), 'error') ?>" name="name" type="text" value="<?= $form->old('name') ?>">

<p class="fields-group">
    <label for="email">Votre adresse email :</label>
    <input id="email" class="field <?= ecco($form->error('email'), 'error') ?>" name="email" type="email" value="<?= $form->old('email') ?>">

<p class="fields-group">
    <label for="message">Le message à m'envoyer :</label>
    <textarea id="message" class="field <?= ecco($form->error('message'), 'error') ?>" name="message"><?= $form->old('message') ?></textarea>

<p class="visually-hidden" aria-hidden="true" id="pot">
<?= csrf_field() ?>
<?= honeypot_field() ?>

<p class="fields-group">
    <input class="custom-checkbox" id="copy" name="receive_copy" type="checkbox" value="true">
    <label for="copy"><span>Cochez cette case si vous souhaitez recevoir une copie de votre message.</span></label>

<p class="form-info"><span class="bold">Information</span> : Tous les champs doivent être remplis.</p>

<p class="fields-group">
    <input type="submit" value="Envoyer" class="btn btn--big btn--white-blue" />

And the controller :


use Uniform\Form;

return function ($site, $pages, $page) {

    $form = new Form([
        'name' => [
            'rules'   => ['required'],
            'message' => 'Notez votre nom.',
        'email' => [
            'rules'   => ['required', 'email'],
            'message' => ['Votre adresse email est nécessaire pour obtenir une réponse.', 'La syntaxe de votre adresse mail semble incorrecte.'],
        'message' => [
            'rules'   => ['required'],
            'message' => 'Veuillez écrire un message.',
        'receive_copy' => [],

    if (r::is('POST')):
        $to         = $site->user(c::get('user'))->email();
        $from_email = $_POST['email'];
        $subject    = 'Contact via […]';
        $message    = $_POST['message'];

            'to'           => $to,
            'from'         => $to,
            'subject'      => $subject,
            'receive-copy' => true,
            'service'      => 'phpmailer',
            'snippet'      => 'form/contact-email-body',

    return compact('form');

I think you should be able to set the checked attribute depending on the old form value, something like:

echo $form->old('receive_copy') == true? 'checked':'';

Ah thanks! I’ve tried without success because i was using === for comparison…

Does it’s safe to do <?= $form->old('receive_copy') ? 'checked': '' ?> ?

Not quite sure what $form->old(‘receive_copy’) actually returns, maybe it is just a string, not a boolean. Then you probably would have to wrap true in quotes. Do a dump to check what you get.

Yes, it’s a string. Thanks again for your help!

I thought so. Then it should be

echo $form->old('receive_copy') == "true"? 'checked':'';
1 Like