function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
adrisseladrissel 

Immediate="true" is not stopping required field validation on VF Page

Here is a simplified version of my page:

<apex:page standardcontroller="Case" extensions="TestPassJSVariable">

    <script type="text/javascript">

        function createCase(cancel) {

            var supplier = document.getElementById('{!$Component.form.block.section1.supplierInput}').value;
            
            if(cancel == 1) {
            
                cancelFunction();
            }

            else {
            
               createFunction(supplier);
            }


    </script>
    <apex:form id="form">
    <apex:actionFunction name="createFunction" action="{!createCase}" rerender="view">    
        <apex:param id="supplier" assignTo="{!supplier}" name="supplier" value="" />  
    </apex:actionFunction>
    <apex:actionFunction name="cancelFunction" action="{!cancelCreate}"/>
    <apex:inputField id="supplierInput" value="{!Case.Supplier__c}" required="true"/>
        <apex:commandButton value="Create" onclick="createCase();" rerender="view"/><apex:commandButton value="Cancel" onclick="createCase(1);" immediate="true"/></center>
    </apex:form>
</apex:page>
I am trying to make the "supplierInput" field required, but also allow a cancel button on the page to go back to the previous URL without having to enter that information in.  I thought the immediate="true" attribute was supposed to allow this to happen, but it isn't working.

What am I missing here?

Best Answer chosen by adrissel
Bhawani SharmaBhawani Sharma
You are using commandButton and actionFunction all together and looks like you only wants to use action function when buttons are clicked.Update your code to following:

//Javascript function
function createCase(cancel) {
////////////////
Exsiting Code
//////////////////////////////
return false;
}

put immediate="true" attribute on your action functions.

<apex:commandButton value="Create" onclick="return createCase();" rerender="view"/>
<apex:commandButton value="Cancel" onclick="return createCase(1);" />


All Answers

AshwaniAshwani
Doing   immedate=true with commandbutton won't help as you are using actionfunction in the end. Use immediate=true with actonfunction. Last call you are doing is by action function and there is no immedate=true attribute there.
adrisseladrissel
That didn't seem to make any difference.  Here is the code now:

<apex:page standardcontroller="Case" extensions="TestPassJSVariable">

    <script type="text/javascript">

        function createCase(cancel) {

            var supplier = document.getElementById('{!$Component.form.block.section1.supplierInput}').value;
            
            if(cancel == 1) {
            
                cancelFunction();
            }

            else {
            
               createFunction(supplier);
            }


    </script>
    <apex:form id="form">
    <apex:actionFunction name="createFunction" action="{!createCase}" rerender="view">    
        <apex:param id="supplier" assignTo="{!supplier}" name="supplier" value="" />  
    </apex:actionFunction>
    <apex:actionFunction name="cancelFunction" action="{!cancelCreate}" immediate="true"/>
    <apex:inputField id="supplierInput" value="{!Case.Supplier__c}" required="true"/>
        <apex:commandButton value="Create" onclick="createCase();" rerender="view"/><apex:commandButton value="Cancel" onclick="createCase(1);" /></center>
    </apex:form>
</apex:page>

I added immediate="true" to the apex:actionFunction and still required all fields when I click the cancel button.

Still something wrong with the code?
Bhawani SharmaBhawani Sharma
You are using commandButton and actionFunction all together and looks like you only wants to use action function when buttons are clicked.Update your code to following:

//Javascript function
function createCase(cancel) {
////////////////
Exsiting Code
//////////////////////////////
return false;
}

put immediate="true" attribute on your action functions.

<apex:commandButton value="Create" onclick="return createCase();" rerender="view"/>
<apex:commandButton value="Cancel" onclick="return createCase(1);" />


This was selected as the best answer
adrisseladrissel
So, we are REALLY close!  Thanks Bhawani, that fixed the Cancel function.  I can now cancel perfectly without any validation errors.  

However, now another issue has arisen.  When I click on the Create button withOUT all the required fields filled in no warning (validation) messages show on the screen.  It doesn't proceed in creating the case, which is good.  But, the little red messages no longer appear under the corresponding fields either.  Here is the code I have now:
<apex:page standardcontroller="Case" extensions="TestPassJSVariable">

    <script type="text/javascript">

        function createCase(cancel) {

            var supplier = document.getElementById('{!$Component.form.block.section1.supplierInput}').value;
            
            if(cancel == 1) {
            
                cancelFunction();
            }

            else {
            
               createFunction(supplier);
            }

       return false;
}
    </script>
    <apex:form id="form">
    <apex:actionFunction name="createFunction" action="{!createCase}" rerender="view">    
        <apex:param id="supplier" assignTo="{!supplier}" name="supplier" value="" />  
    </apex:actionFunction>
    <apex:actionFunction name="cancelFunction" action="{!cancelCreate}" immediate="true"/>
    <apex:inputField id="supplierInput" value="{!Case.Supplier__c}" required="true"/>
        <apex:commandButton value="Create" onclick="return createCase();" rerender="view"/><apex:commandButton value="Cancel" onclick=" return createCase(1);" /></center>
    </apex:form>
</apex:page>

I tried adding immediate="true" to the createFunction as well but that didn't change anything.

Thoughts?

Bhawani SharmaBhawani Sharma
Remove reRender attribute from create action function.
Avidev9Avidev9
You can change the the rerender attribute of createFunction to rerender="form"
and probably can add a page message <apex:pageMessages/>
adrisseladrissel
Marking Bhawani's answer as the best since it contained the original fix, but the full fix is actually a mix of both of yours.  Here is the final working code for anyone else's benefit:

<apex:page standardcontroller="Case" extensions="TestPassJSVariable">

    <script type="text/javascript">

        function createCase(cancel) {

            var supplier = document.getElementById('{!$Component.form.block.section1.supplierInput}').value;
            
            if(cancel == 1) {
            
                cancelFunction();
            }

            else {
            
               createFunction(supplier);
            }

            return false;
        }
    </script>
    <apex:form id="form">
    <apex:actionFunction name="createFunction" action="{!createCase}" rerender="form">    
        <apex:param id="supplier" assignTo="{!supplier}" name="supplier" value="" />  
    </apex:actionFunction>
    <apex:actionFunction name="cancelFunction" action="{!cancelCreate}" immediate="true"/>
    <apex:inputField id="supplierInput" value="{!Case.Supplier__c}" required="true"/>
        <apex:commandButton value="Create" onclick="return createCase();" rerender="view"/><apex:commandButton value="Cancel" onclick=" return createCase(1);" /></center>
    </apex:form>
</apex:page>

Thanks to you both!!