6

Have few Lightning:Input and Lightning:Select components in My Component A. All the fields are having Required attribute as 'true'. Once fields are validated, Field level error (help) messages appear at the bottom of the field and will be there until input Field data is changed (is valid). I have used showHelpMessageIfInvalid() to enable this error message on field explicitly from controller.

I would like to Reset(Hide) the error messages on all these fields from Controller. Is there any way to do this? Looks like "errors" is not a valid attribute for Lightning:Input / Lightning:Select components. Below attempt failed.

component.find('FieldAuraID').set('v.errors', null);

Thanks

  • If you are using the limits like min and max and other features of the lightning input and displaying the messageWhenxxxx using the showHelpMessageIfInvalid then when the field is no longer invalid the message should automatically goes away... – Eric Mar 10 '17 at 5:34
6

If you really wanted to hide error messages you could do something like this:

Component:

<div>
    <lightning:input label="First Name" aura:id="firstName" required="true"/>
    <lightning:button onclick="{!c.hideErrors}">Hide Errors</lightning:button>
</div>

Controller:

hideErrors : function(component, event, helper) {
    var el = component.find("firstName");
    $A.util.removeClass(el, "slds-has-error"); // remove red border
    $A.util.addClass(el, "hide-error-message"); // hide error message
}

Style:

.THIS .hide-error-message > .slds-form-element__help {

.THIS .hide-error-message > .slds-form-element > .slds-form-element__help {
    display: none;
}

But the internal DOM structure and class names of base components are subject to change so this isn't a totally robust solution.

  • For Now, Looks like a good Idea, will try hiding errors when needed and Will enable during re-validation. Thanks @Christophe Coenraets – Sriram Mar 10 '17 at 17:58
  • Also, Added onBlur() function to re-enable css validateField : function(component, event, helper){ var sourceCmp = event.getSource(); if (sourceCmp.get("v.validity").valid) { $A.util.removeClass(sourceCmp, "slds-has-error"); $A.util.addClass(sourceCmp, "hide-error-message"); } else { $A.util.addClass(sourceCmp, "slds-has-error"); $A.util.removeClass(sourceCmp, "hide-error-message"); } } – Sriram Mar 11 '17 at 0:44
  • The old selector doesn't work anymore, added new one. – dzh Jul 31 '18 at 21:16
0

In my case with hidden form the approach below was more appropriate:

<aura:component >
  <aura:attribute name="isVisible" type="Boolean" default="true" />
  <aura:attribute name="field1" type="String" />
  <aura:attribute name="fieldsValidity" type="Object[]" />

  <aura:if isTrue={!v.isVisible}">
    <lightning:input name="textInput" value="{!v.field1}" 
                     validity="{!v.fieldsValidity.field1}" />
  </aura:if>

  <lightning:button label="Clear" onclick={!c.clearErrors} />
</aura:component>

JS:

clearErrors: function(cmp){
   cmp.set('v.fieldsValidity.field1', null);

   cmp.set('v.isVisible', false);
   cmp.set('v.isVisible', true);
}

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.