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
balaji manikandanbalaji manikandan 

How to set Focus in lighting component to the Error field if validation fails? i have tried focus method its not working

Best Answer chosen by balaji manikandan
sfdcMonkey.comsfdcMonkey.com
JavaScript focus() method will not work with ui:inputText field,
however you can show red border, when value is missing, by following way :

if ((appName == null) || (appName == '')) { 
            applicationName.set("v.errors", [{
                message: "Please enter a Product/Application Name"
            }]);
              $A.util.addClass(applicationName, 'slds-has-error');
        }
        else {
             $A.util.removeClass(applicationName, 'slds-has-error');
             applicationName.set("v.errors", null);
        }


OR you can use lightning:input tags, this will support focus() method
<lightning:input label="Name" name="myname" value="{!v.testVal}" messageWhenValueMissing="Please enter a Product/Application Name" aura:id="Product/ApplicationName"/>
in javaScript :
               component.find('Product/ApplicationName').showHelpMessageIfInvalid();
               component.find('Product/ApplicationName').focus();

Kindly let us know if it helps you, & close this query by selecting best answer if you got your solution,
so this will helps other in future

 
sfdcMonkey.com
 

All Answers

balaji manikandanbalaji manikandan
on click on submit im validating my field , but i want to focus the error field
sfdcMonkey.comsfdcMonkey.com
Hi balaji, which type of component you are using ?
<lightning:input> or any aura UI component ?
can you share your code

Thanks
balaji manikandanbalaji manikandan
Hi Piyush,
my component Code: 
    <ui:inputText aura:id="Product/ApplicationName" 
                         value="{!v.objInfo.Product_Application_Name__c}"
                          onError="{!c.handleError}" 
                          onClearErrors="{!c.handleClearError}" 
                           maxlength="50"/>

helper method for valdiation: 

 onProdNameValidation: function(component, event, helper) {
        
        var applicationName = component.find("Product/ApplicationName");
      
        var appName = component.get("v.objInfo.Product_Application_Name__c");
        
        if ((appName == null) || (appName == '')) {
           
            applicationName.set("v.errors", [{
                message: "Please enter a Product/Application Name"
            }]);
            var string = 'Error';
            var err = component.get("v.ErrorString");
            component.set("v.ErrorString", string);
            component.set("v.prodnameboolean", false);
          
        } 
            else {
           
            applicationName.set("v.errors", null);
            var err = applicationName.get("v.errors");
            if (err == null) {
                component.set("v.prodnameboolean", true);
            }
        }
    },

I have tried focus() method its not wrking ?
sfdcMonkey.comsfdcMonkey.com
JavaScript focus() method will not work with ui:inputText field,
however you can show red border, when value is missing, by following way :

if ((appName == null) || (appName == '')) { 
            applicationName.set("v.errors", [{
                message: "Please enter a Product/Application Name"
            }]);
              $A.util.addClass(applicationName, 'slds-has-error');
        }
        else {
             $A.util.removeClass(applicationName, 'slds-has-error');
             applicationName.set("v.errors", null);
        }


OR you can use lightning:input tags, this will support focus() method
<lightning:input label="Name" name="myname" value="{!v.testVal}" messageWhenValueMissing="Please enter a Product/Application Name" aura:id="Product/ApplicationName"/>
in javaScript :
               component.find('Product/ApplicationName').showHelpMessageIfInvalid();
               component.find('Product/ApplicationName').focus();

Kindly let us know if it helps you, & close this query by selecting best answer if you got your solution,
so this will helps other in future

 
sfdcMonkey.com
 
This was selected as the best answer