Uniform plugin – Keep checkboxes status on error

Hello,

i’m currently working on a form based on uniform for kirby 3 and i’m – still :roll_eyes: – too stupid for some details:

The form works fine, but if it displays an error message – because some required field is empty – the information in the input fields remains, but the status of already “checked” checkboxes is deleted.

What do I have to do so that the checkboxes keep their status?

Here is my code:

Part of the template

<div class="text-input">
    <label>Name</label>
    <input class="input-field<?php e($form->error('name'), ' error'); ?>" name="name" type="text" value="<?php echo $form->old('name') ?>">
    <?php snippet('form/error', ['field' => 'name']) ?>
</div>

<div class="checkboxes">
    <input <?php e($form->hasError('topics'), 'class="error"')?> type="checkbox" id="Topic-ABC" value="Topic-ABC" name="topics[]">
    <label for="Topic-ABC">Topic-ABC</label>

    <input <?php e($form->hasError('topics'), 'class="error"')?> type="checkbox" id="Topic-XYZ" value="Topic-XYZ" name="topics[]">
    <label for="Topic-XYZ">Topic-XYZ</label>

    <?php snippet('form/error', ['field' => 'topics']) ?>
</div>  

Part of the controller

use Uniform\Form;

$form = new Form([

    'name' => [
        'rules' => ['required'],
        'message' => 'Please enter your name.',
    ],

    'topics' => [
        'rules' => ['required'],
        'message' => 'Please select at least one topic.',
    ],
]);

Part of the email snippet

<?php $checkboxes = !empty($_POST['topics']) ? $_POST['topics'] : array();
echo implode(' | ', $checkboxes) ?>

I found this topic Uniform plugin - Keep receive_copy checkbox checked on error, but it didn’t really get me anywhere.

I would be very happy about some help. :slight_smile:

I assume

$form->old('topics')

gives you an array. So you would probably have to check against something like

$form->old('topics')[0]

etc. to set the checked attribute.

Do a

dump($form->old('topics'));

to check what that gives you.

I’ve changed it a bit. Now one checkbox keeps its status, all others are un-checked on error…

<?php $value = $form->value('topics') ?>

<input <?php e($form->hasError('topics'), 'class="error"')?> type="checkbox" name="topics" id="<?= $topics[0]; ?>" value="<?= $topics[0]; ?>"<?php echo $form->old('topics') == $topics[0]? 'checked':''; ?> />
<label for="<?= $topics[0]; ?>"><?= $topics[0]; ?></label>

<input <?php e($form->hasError('topics'), 'class="error"')?> type="checkbox" name="topics" id="<?= $topics[1]; ?>" value="<?= $topics[1]; ?>"<?php echo $form->old('topics') == $topics[1]? 'checked':''; ?> />
<label for="<?= $topics[1]; ?>"><?= $topics[1]; ?></label>

<input <?php e($form->hasError('topics'), 'class="error"')?> type="checkbox" name="topics" id="<?= $topics[2]; ?>" value="<?= $topics[2]; ?>"<?php echo $form->old('topics') == $topics[2]? 'checked':''; ?> />
<label for="<?= $topics[2]; ?>"><?= $topics[2]; ?></label>

<input <?php e($form->hasError('topics'), 'class="error"')?> type="checkbox" name="topics" id="<?= $topics[3]; ?>" value="<?= $topics[3]; ?>"<?php echo $form->old('topics') == $topics[3]? 'checked':''; ?> />
<label for="<?= $topics[3]; ?>"><?= $topics[3]; ?></label>

<?php snippet('form/error', ['field' => 'topics']) ?>

Controller:

$topics = array (
    'topicone',
    'topictwo',
    'topicthree',
    'topicfour',
    'topicfive'
);

$form = new Form([

    'topics' => [
        'rules' => ['required'],
        'message' => 'Please select at least one topic.',
    ],
]);

Again, what do you get if you dump

dump(get('topics'));

Nothing.

Or

dump($_POST['topics']);

Should return something after sending the form.

It returns the checked checkboxes after sending the form:

<pre>Array
(
    [0] => topicone
    [1] => topictwo
)
</pre>

Ok, we have to solve this with in_array(), something like:

<?= in_array($topics[0], $form->old('topics'))? 'checked':''; ?>

(assuming that $form->old('topics') is the same array as $_POST['topics'])

I’m so sorry :see_no_evil:, but I get this error message:

in_array() expects parameter 2 to be array, string given

I thought $form->old('topics') returned an array, seems it doesn’t. Try $_POST['topics'] then.

Already tried it. :wink: Still not working. You can give up my case if you want, I wasn’t expecting it to be so complicated… :smiley:

I just realized that your problem is the name of your inputs, should be

name="topics[]"

In the end it should look something like this:

<input type="checkbox" name="topics[]" id="blue" value="blue" <?= isset($_POST['topics']) && in_array('blue', $_POST['topics']) ? 'checked':'' ?> />
<label for="blue">Blue</label>

<input  type="checkbox" name="topics[]"  id="green" value="green" <?= isset($_POST['topics']) && in_array('green', $_POST['topics']) ? 'checked':'' ?> />
<label for="green">Green</label>

<input  type="checkbox" name="topics[]"  id="red" value="red" <?= isset($_POST['topics']) && in_array('red', $_POST['topics']) ? 'checked':'' ?> />
<label for="red">Red</label>

<input type="checkbox" name="topics[]"  id="black" value="black" <?= isset($_POST['topics']) && in_array('black', $_POST['topics']) ? 'checked':'' ?> />
<label for="black">Black</label>
</div>
<input type="submit" name="submit" value="Submit">

Guess you can then also use $form->old('topics') instead of `$_POST[‘topics]’ because that should now return an array.

Check out this solution here: Kirby Uniform Plugin: Multiple Checkbox

Aaaah, crazy, it’s working! :partying_face:

I’ve just sat down at my computer to follow your last advice - but this solution here has led me straight to my goal.

Thank you soooo much and have a nice weekend!

1 Like