0

I am using a VF page having a custom controller. For simplicity, I am using 2 date fields - fromDate and toDate which belongs to a custom object.

There is a command button named Search on VF page which when clicked calls a javascript function to check if fromDate is greater than toDate.

Now the problem is when I enter "adbcg" (some garbage) in fromDate and some legitimate date in toDate and hit the search button, javascript function is getting called and not the system validation for date field. I first want the system validation to fire as in the fromDate should be highlighted to give Invalid date error.

Any help highly appreciated.

VF page---

<apex:page controller="FinancialSearchController" showheader="true">    
<script type="text/javascript">

        function checkValidations() {
            ....
            ...
        } 
 <script type="text/javascript">
 <apex:form id="FinancialDataSearchForm" > 
    <apex:outputLabel value="{!$ObjectType.Financial_Search__c.fields.fromDate__c.label}" for="priority"/>
    <apex:inputField id="fromDate" value="{!FSInputRecord.fromDate__c}"/>
    <apex:commandButton value="Search" onclick="checkValidations(); return false;" reRender="primaryResultsPanel, additionalResultsPanel"/>
 </apex:form >
0

I think you can create action function that saves record and runs system validations and have your JS validation in oncomplete button. However it is bad practice to have those mixed. You can have validation on server side. Try to use maximum out of apex:inputField to have better validation. For example to put JS function in onchange event. https://developer.salesforce.com/docs/atlas.en-us.pages.meta/pages/pages_compref_inputField.htm

  • I don't want to save the record. Can we achieve it without committing to Database? – Prasadium Aug 23 '17 at 21:06
  • If you look at the Apex Order of Execution, you'll see that validation rules are run before the record is saved, so if it fails validation then it won't get saved to the system. If you don't want to save the record under any circumstances, then custom validation rules aren't the way to go. developer.salesforce.com/docs/atlas.en-us.apexcode.meta/… – Jeff Bennett Aug 23 '17 at 21:36
  • 1
    You can delete it immediately a after save. Or you can try to use database.setsavepoint and rollback method to rollback commit – pklochkov Aug 23 '17 at 21:41
  • @JeffBennett I haven't written any custom validation anywhere except for the Javascript function validation which sits on the VF page itself. Javascript function callouts within the page doesn't come anywhere for salesforce order of execution. pklochkov this would be my last resort ;) – Prasadium Aug 23 '17 at 23:21

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.