2

I have two pageBlocks on the page, one of which I want to validate/submit only when a checkbox is checked. The problem I run into is that validation It has the following javascript:

<script>
    function showHideAccount(input, blockId) {
        accountBlockDiv = document.getElementById(blockId);
        if (!input.checked) {
            accountBlockDiv.style.display = 'none';
        } else {
            accountBlockDiv.style.display = 'block';
        }
    }
</script>

This is my checkbox:

<apex:inputCheckbox value="{!accountChecked}" id="accountChecked" onclick="showHideAccount(this, '{!$Component.accountBlock}')" />

and this is the pageBlock I want to hide/show:

<apex:pageBlockSection columns="1" id="accountBlock">...

The problem is that it has required fields, and when I hide it, it still attempts to validate. Also it seems like the checkbox boolean value (accountChecked) does not reflect the controller extension's accountChecked value.

I tried doing a rerender on the page when the checkbox is clicked with rendered="{!accountChecked}" and required="{!accountChecked}" on the required fields that cause the issue, but IT DOESN'T DO ANYTHING. Help?

2

To avoid validation while using reRender, set immediate="true" on the element that fires the reRender event (e.g. commandButton, commandLink, etc). Also, you can use an actionRegion to prevent validation on elements outside the region, which can often resolve this problem.

  • 1
    Beyond limiting the scope of the validation, action region sends less data back up (only its contents instead of the whole form) so it's faster. – Shane McLaughlin Jan 22 '14 at 5:05
  • I added immediate="true" which skips the validation, but now the conditional render doesn't work. When I click the element that fires the reRender, it looks like the page just changes left/right margins on some elements. – Eric Kim Jan 23 '14 at 14:55

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.