Picture a form with 4 sections. I wanted to allow the users to select which of those 4 sections he wants to fill in (if not all 4 of them) so what I did was:
I wrapped each section into a div with an id from 1 to 4:
Created a checkbox linked to those four divs that shows/hides them if checked/unchecked:
Handle everything via JS:
Everything works just as you would imagine, that is until you add required='1' to your forms. It looks like if parts of the hidden forms are required, they will also through in an error requiring you to fill them.
First I thought about wrapping/unwrapping the divs dynamically with the ignore tag but that won't work cause JS can't wrap a div in <cms:ingore></cms:ignore> and unwrap it. Using the eighth node to comment/uncomment the divs is a pain in the **ahem too.
Is there another way around this ?
For the moment I have no choice but to leave all the fields with required='0'.
I wrapped each section into a div with an id from 1 to 4:
- Code: Select all
<div id="div-1" class="hidden">Form 1</div>
<div id="div-2" class="hidden">Form 2</div>
<div id="div-3" class="hidden">Form 3</div>
<div id="div-4" class="hidden">Form 4</div>
Created a checkbox linked to those four divs that shows/hides them if checked/unchecked:
- Code: Select all
<input type="checkbox" name="group[]" class="show-div" data-target="div-1" value="1">
<input type="checkbox" name="group[]" class="show-div" data-target="div-2" value="2">
<input type="checkbox" name="group[]" class="show-div" data-target="div-3" value="3">
<input type="checkbox" name="group[]" class="show-div" data-target="div-4" value="4">
Handle everything via JS:
- Code: Select all
$('input.show-div').on('change', function() {
var source = $(this);
var target = $('#' + source.attr('data-target'));
if ($('input[data-target='+source.attr('data-target')+']:checked').length) target.show();
else target.hide();
});
Everything works just as you would imagine, that is until you add required='1' to your forms. It looks like if parts of the hidden forms are required, they will also through in an error requiring you to fill them.
First I thought about wrapping/unwrapping the divs dynamically with the ignore tag but that won't work cause JS can't wrap a div in <cms:ingore></cms:ignore> and unwrap it. Using the eighth node to comment/uncomment the divs is a pain in the **ahem too.
Is there another way around this ?
For the moment I have no choice but to leave all the fields with required='0'.