Multiple <option> required on <select>

Viewed 29

There's a lot of members to be choosen in option field. How can I be sure that at least 2 has been selected? The first option is required, I need the second to be also required

Here's the code:

    <select name="presents[]" id="presents" class="vv-select2-multiple" multiple required>
        <?php foreach(get_from_group('person', 'any', -1, ['tipo' => 'member']) as $member):?>
            <option value="<?= $member->ID ?>" <?php selected(get_user_person()->ID, $member->ID)?> required><?= $member->post_title; ?></option>
            <?php endforeach;?>
    </select>
1 Answers

Your select tag presents is an array already. Check the array length to make sure you have at least 2 options selected. When the form is submitted, try this:

$presents = $_POST['presents'];
if (count($presents) >= 2) {
    // Here you are sure that you have at least two options selected.
    // Do whatever you wish here. 
}
Related