[SalesForce] lightning:input Clear validation error messages

I have enabled error messages (validation failed) for Lightning:input components inside my lightning component A using
inputCmp.showHelpMessageIfInvalid()

I am trying to clear the Lightning:input error "messages" when I re-open the same lightning component A (show/hide using css).
I Could clear Red colored border But couldn't clear error message below the text field. Please suggest how to clear default error message.
inputCmp.set('v.messageWhenValueMissing', ''); is not working.

Error message as expected after validation

Want to clear this error message(reset input component)

clearValidationErrors : function(component, fieldSet) {
    for (var field in fieldSet) {
        if (fieldSet.hasOwnProperty(field)) {
            var inputCmp = component.find(field);
            if(inputCmp) {
                $A.util.removeClass(inputCmp, "slds-has-error");
                $A.util.addClass(inputCmp, "hide-error-message");
                //Clear Error Message (Ex: "Complete this field")
                //Trying to achieve something like below
                     //inputCmp.set("v.privateHelpMessage", ""); //But it is Private :(

            }
        }
    }
}

Thanks for your time..

Best Answer

we had a similar requirement and the way we fixed it was to put the entire form inside an aura:if and changed the Boolean condition. this essentially meant the dom got re-created again but we were able to reset 4 to 5 fields in one condition.

<aura:component >
    <aura:attribute name="truthy" default="true" type="boolean"/>
    <lightning:button label="Click Me" onclick="{!c.clickMe}" />
     <aura:if isTrue="{!v.truthy}">
    <lightning:input type="text" name="firstName" required="true" aura:id="firstName" label="First Name" value="{!v.firstName}" 
    messageWhenValueMissing="First name is required"/>
    </aura:if>     
</aura:component>

Helpercode:

compCreate:function(com,eve,helper){
   com.set('v.truthy',false);
      com.set('v.truthy',true);
}

We also did not have the need to touch each field this way instead controlled everything through an aura:if.Not the cleanest or most-efficient solution but till we get a reset function in lightning-input in the future we will use this :)

Related Topic