0

I have this form.

    <?php
// No direct access to this file
defined('_JEXEC') or die('Restricted access');

JHtml::_('behavior.keepalive');
JHtml::_('behavior.formvalidation');
JHtml::_('behavior.tooltip');
?>

<form class="form-validate"
    action="<?php echo JRoute::_('index.php'); ?>" method="post"
    id="register" name="register" style="padding-bottom: 20px;">
    <fieldset>
            <div><?php echo JText::_('COM_STRBIOMERIEUX_FORM_TITLE'); ?> <span style="font-weight: bold;"><?php if (JFactory::getApplication()->input->get('journey', '') == 'j') echo JText::_('COM_STRBIOMERIEUX_JOURNEYS'); elseif (JFactory::getApplication()->input->get('journey', '') == 'w') echo JText::_('COM_STRBIOMERIEUX_WORKSHOPS'); ?></span></div>
            <hr />
            <div style="font-size: 0.8em;"><?php echo JText::_('COM_STRBIOMERIEUX_FORM_DESC'); ?></div>
            <hr />
            <?php echo $this->form->getInput('journey','',$this->journey->id); ?>
        <dl>
            <dt><?php echo $this->form->getLabel('name'); ?></dt>
            <dd><?php echo $this->form->getInput('name'); ?></dd>

            <dt><?php echo $this->form->getLabel('nclient'); ?></dt>
            <dd><?php echo $this->form->getInput('nclient'); ?></dd>

            <dt><?php echo $this->form->getLabel('name_lab'); ?></dt>
            <dd><?php echo $this->form->getInput('name_lab'); ?></dd>

            <dt><?php echo $this->form->getLabel('function'); ?></dt>
            <dd><?php echo $this->form->getInput('function'); ?></dd>

            <dt><?php echo $this->form->getLabel('service'); ?></dt>
            <dd><?php echo $this->form->getInput('service'); ?></dd>

            <dt><strong>Dados para recibo</strong></br>
            </br></dt>
            <dd></br></dd>

            <dt><?php echo $this->form->getLabel('address'); ?></dt>
            <dd><?php echo $this->form->getInput('address'); ?></dd>

            <dt><?php echo $this->form->getLabel('postal_code'); ?></dt>
            <dd><?php echo $this->form->getInput('postal_code') . ' - ' . $this->form->getInput('postal_code_3') . ' ' . $this->form->getInput('postal_code_desc'); ?></dd>

            <dt><?php echo $this->form->getLabel('phone'); ?></dt>
            <dd><?php echo $this->form->getInput('phone'); ?></dd>

            <dt><?php echo $this->form->getLabel('fax'); ?></dt>
            <dd><?php echo $this->form->getInput('fax'); ?></dd>

            <dt><?php echo $this->form->getLabel('email'); ?></dt>
            <dd><?php echo $this->form->getInput('email'); ?></dd>

            <dt><?php echo $this->form->getLabel('receipt'); ?></dt>
            <dd><?php echo $this->form->getInput('receipt'); ?></dd>
        </dl>
        <dl id="receiptname-hide">
            <dt><?php echo $this->form->getLabel('receiptname'); ?></dt>
            <dd><?php echo $this->form->getInput('receiptname'); ?></dd>
        </dl>
        <dl>
            <dt><?php echo $this->form->getLabel('nif'); ?></dt>
            <dd><?php echo $this->form->getInput('nif'); ?></dd>

            <dt><?php echo $this->form->getLabel('observations'); ?></dt>
            <dd>Coloque aqui o Titular de Conta</br><?php echo $this->form->getInput('observations'); ?></dd>

            <dt><label id="jform_select-lbl" for="jform_select" class=""><?php if (JFactory::getApplication()->input->get('journey', '') == 'j') echo JText::_('COM_STRBIOMERIEUX_JOURNEYS'); elseif (JFactory::getApplication()->input->get('journey', '') == 'w') echo JText::_('COM_STRBIOMERIEUX_WORKSHOPS'); ?>:</label></dt>
            <dd>
                <fieldset id="jform_select" class="checkbox">
                <?php
                if (JFactory::getApplication()->input->get('journey', '') == 'w') {
                    $for = $this->works;
                } else {
                    $for = $this->journeys;
                }
                $xxid = JFactory::getApplication()->input->get('wid', 0);
                ?>
                    <?php foreach ($for as $k => $v) { ?>
                    <input type="checkbox" id="jform_select<?php echo $k;?>" name="jform[select][]" value="<?php echo $v->id;?>" <?php if(($xxid == 0 AND $k == 0) OR ($xxid != 0 AND $xxid == $v->id)) echo 'checked="checked"'; ?>/><label style="max-width: 370px;" for="jform_select<?php echo $k;?>"><?php echo $v->name;?> - <?php if (JFactory::getApplication()->input->get('journey', '') == 'j') echo JText::_('COM_STRBIOMERIEUX_JOURNEYS2'); elseif (JFactory::getApplication()->input->get('journey', '') == 'w') echo JText::_('COM_STRBIOMERIEUX_WORKSHOPS2'); ?></label>
                    <input type="hidden" id="jform_selectname<?php echo $v->id;?>" name="jform[selectname<?php echo $v->id;?>]" value="<?php echo $v->name;?>" />
                    <?php if($v->date_event != '0000-00-00' AND $v->event_canceled != '1') { ?>
                    <label class="inline" for="jform_selectx<?php echo $v->id;?>"><input class="inline" type="radio" id="jform_selectx<?php echo $v->id;?>" name="jform[selectx<?php echo $v->id;?>]" value="Lisboa" checked="checked" />Lisboa</label>
                    <?php } if($v->date_event2 != '0000-00-00' AND $v->event_canceled2 != '1') { ?>
                    <label class="inline" for="jform_selectxx<?php echo $v->id;?>"><input class="inline" type="radio" id="jform_selectxx<?php echo $v->id;?>" name="jform[selectx<?php echo $v->id;?>]" value="Porto" />Porto</label>
                    <?php } ?>
                    <hr />
                    <?php } ?>
                </fieldset>
            </dd>

            <dt></dt>
            <dd></dd>
            <dt></dt>
            <dd></br>
                <input type="hidden" name="option" value="com_strbiomerieux" /> <input
                    type="hidden" name="task" value="register.submit" />

            </dd>
            <dt><strong>Pagamentos</strong></dt>
            <dd>Nome do Beneficiário:</dd>
            <dt></dt>
            <dd>
            <span style="display: none;"><button id="frmsubmit" type="submit" class="validate"><?php echo JText::_('JSUBMIT'); ?></button></span>
                <button type="button" onclick="document.getElementById('frmsubmit').click();" class="button"><?php echo JText::_('JSUBMIT'); ?></button>

                <button type="reset" class="button"><?php echo JText::_('JCANCEL'); ?></button>
                            <?php echo JHtml::_('form.token'); ?>
                </dd>
                <noscript>
                <dt>Aviso:</dt>
                <dd>JavaScript do seu browser está desativado!<br />O site poderá não funcionar corretamente!<br />para conseguir fazer o registo ative o Javascript.</dd>
                </noscript>
        </dl>
        <fieldset>

</form>
<div class="clr"></div>

I have this part

  <dt><label id="jform_select-lbl" for="jform_select" class=""><?php if (JFactory::getApplication()->input->get('journey', '') == 'j') echo JText::_('COM_STRBIOMERIEUX_JOURNEYS'); elseif (JFactory::getApplication()->input->get('journey', '') == 'w') echo JText::_('COM_STRBIOMERIEUX_WORKSHOPS'); ?>:</label></dt>
                <dd>
                    <fieldset id="jform_select" class="checkbox">
                    <?php
                    if (JFactory::getApplication()->input->get('journey', '') == 'w') {
                        $for = $this->works;
                    } else {
                        $for = $this->journeys;
                    }
                    $xxid = JFactory::getApplication()->input->get('wid', 0);
                    ?>
                        <?php foreach ($for as $k => $v) { ?>
                        <input type="checkbox" id="jform_select<?php echo $k;?>" name="jform[select][]" value="<?php echo $v->id;?>" <?php if(($xxid == 0 AND $k == 0) OR ($xxid != 0 AND $xxid == $v->id)) echo 'checked="checked"'; ?>/><label style="max-width: 370px;" for="jform_select<?php echo $k;?>"><?php echo $v->name;?> - <?php if (JFactory::getApplication()->input->get('journey', '') == 'j') echo JText::_('COM_STRBIOMERIEUX_JOURNEYS2'); elseif (JFactory::getApplication()->input->get('journey', '') == 'w') echo JText::_('COM_STRBIOMERIEUX_WORKSHOPS2'); ?></label>
                        <input type="hidden" id="jform_selectname<?php echo $v->id;?>" name="jform[selectname<?php echo $v->id;?>]" value="<?php echo $v->name;?>" />
                        <?php if($v->date_event != '0000-00-00' AND $v->event_canceled != '1') { ?>
                        <label class="inline" for="jform_selectx<?php echo $v->id;?>"><input class="inline" type="radio" id="jform_selectx<?php echo $v->id;?>" name="jform[selectx<?php echo $v->id;?>]" value="Lisboa" checked="checked" />Lisboa</label>
                        <?php } if($v->date_event2 != '0000-00-00' AND $v->event_canceled2 != '1') { ?>
                        <label class="inline" for="jform_selectxx<?php echo $v->id;?>"><input class="inline" type="radio" id="jform_selectxx<?php echo $v->id;?>" name="jform[selectx<?php echo $v->id;?>]" value="Porto" />Porto</label>
                        <?php } ?>
                        <hr />
                        <?php } ?>
                    </fieldset>
                </dd>

That gives me this

enter image description here

I want to make one of the select fields to be required, even if don't exist any of the items (the form doesn't submit)

I can make all the fields required (where Micologia and HIV works, but I select the two square boxes), but I only want one to be select, and not two.

1

Make the checkbox a radio button instead of checkbox, make the radio button required, and then follow this guide to make it look the radio button look like a checkbox.

  • I'd agree with the radio button approach. I suggest not styling a radio button as a checkbox, though - people will expect a radio button to behave in the manner wanted. Making a radio button act like a checkbox isn't good for usability because the behaviour is then unexpected. – Richard B Dec 4 '16 at 19:31

Your Answer

By clicking "Post Your Answer", you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.