We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

Checkboxes In Table

Hello. I have a loop that creates multiple check boxes with the same name.

foreach ($openTickets as $ticketId)
        {
            $checkbox = new Check('id-'.$ticketId['TicketId'], array("name" => "tickets[]", "id" => 'id-'.$ticketId['TicketId'] , "value" => $ticketId['TicketId']));
              $checkbox->addValidators(array(
                    new PresenceOf(array(
                        "message" => 'Please select a ticket'
                    ))
                )
            );
                $this->add($checkbox);
        }

and on the phtml side.

foreach ($openTickets as $ticket) {?>
                    <tr >
                        <td>
                            <?php echo $form->render('id-'.$ticket['TicketId'])?>
                        </td>
                        <td><?php echo $ticket['DateCreatedDifference']; ?></td>
                    </tr>
                <?php }?>

I am trying to force at least one checkbox to be checked. How do I achieve this.

Post fixed. Please read the Markdown FAQ for properly formatting code.



17.5k

I don't use Volt, so it may not be 100% correct, but something like...

    $checked = false;
    foreach($openTickets as $ticketId) {
        $checkbox = new Check(...);
        if (!$checked) {
            $checkbox['checked'] = true;
            $checked = true;
        }
        $checkbox->addValidators(...);
        $this-add($checkbox);
    }