You need to sign in to do that
Don't have an account?
![Surya Kiran Surya Kiran](https://dfc-org-production.my.site.com/img/userprofile/default_profile_45_v2.png)
setCustomValidity not working for lightning:input date fields
Hi Folks,
I have set a custom error message using setCustomValidity() in lightning, while clearing it is not working for lightning:input date fields. Seems it is issue from Salesforce. Please let me know if there is any workaround.
Here is the example:
Lightning Component:
Javascript Controller:
I have set a custom error message using setCustomValidity() in lightning, while clearing it is not working for lightning:input date fields. Seems it is issue from Salesforce. Please let me know if there is any workaround.
Here is the example:
Lightning Component:
<aura:component implements="force:appHostable" > <lightning:card footer="Card Footer" title="Hello"> <lightning:input Label="Name" aura:id="name" value="{!v.acc.Name}" required="true"/> <lightning:input type="date" aura:id="date" label="Enter a date" required="true" value="{!v.acc.dhr__Custom_Date_Time__c}"/> <lightning:input type="date" label="Enter a date" required="true" value="{!v.acc.dhr__SLA_Expiration_Date_Time__c}"/> <lightning:button variant="brand" label="Handle Form" title="Brand action" onclick="{! c.handleForm }" /> </lightning:card> </aura:component>
Javascript Controller:
({ handleForm : function(component,event,helper){ var nameCmp = component.find("name"); if(!$A.util.isEmpty(component.get("v.acc.Name")) && component.get("v.acc.Name")=="Salesforce"){ nameCmp.setCustomValidity("Name cannot be Salesforce") ; }else{ nameCmp.setCustomValidity("") ; } nameCmp.reportValidity() ; var dateCmp = component.find("date"); if(component.get("v.acc.dhr__Custom_Date_Time__c") > component.get("v.acc.dhr__SLA_Expiration_Date_Time__c")){ dateCmp.setCustomValidity("Cannot be future..") ; }else{ alert('clearing..'); dateCmp.setCustomValidity("") ; } dateCmp.reportValidity() ; } })
https://developer.salesforce.com/docs/atlas.en-us.lightning.meta/lightning/js_validate_fields.htm?search_text=onClearErrors
Please try to remove required="true" attribute and set the custom message in client controller. Its showing custom error messages when I tried it.
Recently I have also faced the same issue. And found the solution.
You need to mention "auraid.reportValidity();" to show the message immediately.
Please mark as best answer if this solves your problem.
Try the Code it Works fine.Please change it with your requirnment. I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.
Thanks,
Ajay Dubedi