Required field validation in Lightning Component.

How to apply required field validation in lightning component ?

Lets learn it by an example. In my case I am having a <ui:input> name oppName and a lightning button in my lightning component.

To apply the validation we have to write code on click of lightning button.

Here is the code below : 

buttonClick : function(component){
var oppName = component.find("oppName");
       
        if(oppValue == null){
            oppName.set("v.errors",[{message:"You must enter a value"}]);
        }
        else{
            oppName.set("v.errors", null);
              }
}

In this above example first I get my component in a variable oppName. After that I am checking that if its value is Null.

And if the value is null then I am setting the error message using v.error in set function.

Once the value if filled again than it is necessary to make error attribute null again in else part otherwise it will keep showing the error.

Share this post with your friends if this helps you somehow. 

Post a Comment

0 Comments