2

I currently have a Visualforce page where a User can tick selection boxes in order to edit multiple License Voucher # Object fields as shown below.

enter image description here

I would like to perform a field check for empty fields on every record added to the list. This field validation would be done when the user clicks on the Add Voucher to Project button.

Currently what I have setup is for the actionButton to call a javaScript function onClick which would retrieve the boolean result from my validateForm() method in my controller.

However I am unsure how to return a boolean value from my controller back to my javaScript function in order to proceed with updating the records (It always returns true).

Here's a bit of the code so far:

View:

<apex:outputPanel layout="block" id="resultPanel" style="overflow:auto;width;250px;height:300px">
           <apex:commandButton value="Add Vouchers to Project" action="{!addMultiLRtoProject}" onclick="return validateForm()"/>
           <apex:commandButton value="Cancel" action="{!backToProject}"/>

           <apex:pageBlockTable value="{!wrapSelectedList}" var="c" id="resultTable" title="Selected Accounts">
                <apex:column value="{!c.lrObj.License_Voucher__c}" headerValue="Voucher Number"/>

                <apex:column headerValue="Server Name">
                    <apex:inputText id="inputServerName"    value="{!c.serverName}"/><br/>
                </apex:column>

                <apex:column headerValue="Serial Number">     
                    <apex:inputText id="inputSerialNumber"  value="{!c.serialNumber}"/><br/>
               </apex:column>

               <apex:column headerValue="MAC Address">                   
                   <apex:inputText id="inputMACAddress"    value="{!c.macAddress}"/><br/>
               </apex:column>

               <apex:column headerValue="Server Model">
                   <apex:inputText id="inputServerModel"   value="{!c.serverModel}"/><br/>
               </apex:column>

            </apex:pageBlockTable>
       </apex:outputPanel>

JavaScript in the view:

        function validateForm()
        {
            var valid = "{!validForm}";                
            alert(valid);                
            return valid;
        }

Controller Top declaration:

public boolean validForm { get { return validateFields(); } }

Controller validateFields Method:

public boolean validateFields()
{
    List<Olympus_Voucher__c> validList = new List<Olympus_Voucher__c>();
    validList = Model_OlympusLR_Utility.setMultiVouchersToProject(wrapSelectedList);
    system.debug('GOT HERE');
    errorCount=0;

    for(Olympus_Voucher__c checkObjFields : validList)
    {
        if(String.isEmpty(checkObjFields.Server_Name_ID__c))
        {
            errorCount++;
            system.debug('ServerName');

        }           
        if(String.isEmpty(checkObjFields.Serial_Number__c))
        {
            errorCount++;
            system.debug('Serial Number');
        }
        if(String.isEmpty(checkObjFields.MAC_Address__c))
        {
            errorCount++;
            system.debug('MAC HERE');
        }
        if(String.isEmpty(checkObjFields.Server_Name_ID__c))
        {
            errorCount++;
            system.debug('Obj');
        }  
    }//END FOR LOOP 
    if(errorCount > 0) //NUMBER OF ERRORS
    {
        return false; //TEST FAILED

    }
    else
    {
        return true; //TEST PASSED
    }
    return false; //DEFAULT SET TO FAIL
}
1

Why don't you call your validate method before you actually add a row, all in your controller? In your addMultiLRtoProject method, do the following:

public void addMultiLRtoProject()
{
    if (!validateFields())
    {
        // throw an error on the page 
        ApexPages.addMessage(new ApexPages.message(ApexPages.severity.ERROR, 'Validation Failed or whatever'));
        return;
    }

    // your existing code to add new row
}

and in your page:

<apex:pageMessages id="messages" />
<apex:commandButton value="Add Vouchers to Project" action="{!addMultiLRtoProject}" />
  • 1
    That actually makes a lot more sense doing it this way now that I see it. Works well when I implemented your approach! Thank you very much. – enph Aug 15 '14 at 2:23

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.