I have added new fields in the configuration file for my form, like this:
<?xml version="1.0" encoding="utf-8"?>
<form>
<fieldset>
<field name="yes" type="checkbox" label="Answer yes" />
<field name="options" type="list" label="Type" description="">
<option value="" selected="selected">default</option>
<option value="Option1">Option1</option>
<option value="Opcion2">Option2</option>
</field>
</fieldset>
</form>
In the view, I show the fields:
<div class="control-group">
<div class="control-label">
<?php echo $this->form->getLabel('yes'); ?>
</div>
<div class="controls">
<?php echo $this->form->getInput('yes'); ?>
</div>
</div>
<div class="control-group" id="div_options" style="display: none;">
<div class="control-label">
<?php echo $this->form->getLabel('options'); ?>
</div>
<div class="controls">
<?php echo $this->form->getInput('options'); ?></div>
</div>
</div>
And with Javascript (jQuery), I have set the condition that when the checkbox is checked, I show the select with options. But I need to validate with Joomla that just in case the checkbox is checked, the selection is mandatory and the user must select an option.
if (jQuery('#jform_yes').is(':checked')) {
jQuery("#div_copciones").show();
jQuery("#jform_options_chzn").attr('required', true);
jQuery("#jform_options_chzn").addClass('validate-options');
}
jQuery('#jform_yes').on('change', function(){
if (this.checked) {
jQuery("#div_opciones").show();
jQuery("#jform_options_chzn").attr('required', true);
jQuery("#jform_options_chzn").addClass('validate-options');
} else {
jQuery('#jform_yes').val(0);
jQuery("#jform_options_chzn").attr('required', false);
jQuery("#div_options").hide();
}
});
How can I validate that the user chooses an option in the select before sending the form? Is my process correct? Is there any better way to do it? Thanks!