0

I'm building a component and I would like a field to appear only if it has not been filled in. It's working fine to hide when the field isn't empty, but when I need to fill it using my component interface and I start typing, the field disappears from the component screen. Am I missing something or is this expected behavior?

<aura:if isTrue="{!v.accounts.OnshoreWeight__c == null }">
         <lightning:input name="onshore"  
                         aura:id="client"
                         label="Estrutura On"
                         class="form-control"
                         value="{!v.accounts.OnshoreWeight__c}"
                         placeholder=""/>
</aura:if>

1 Answer 1

0

Yes, that's expected. The very moment you type something, it triggers an aura:valueChange event that will cause the ifTrue to be re-evaluated, and the field disappears, typically within one or two keystrokes.

You'll want to set an attribute to control if the field is shown:

<aura:attribute name="showOnshoreWeight" type="Boolean" default="{!false}" />

<aura:if isTrue="{!v.showOnshoreWeight}">

And then control if it should be set on record load/save:

<force:recordData recordUpdated="{!c.resetUI}" ... />

resetUI: function(component, event, helper) {
  const record = component.get("v.accounts");
  component.set("v.showOnshoreWeight", !record.OnshoreWeight__c);
}

This way, the field will only be shown when the record is loaded.

4
  • Hi sfdcfox, thank you for yours answer. My question is: where can I create my condition to show it only if the OnshoreWeight__c field is equal to NULL?
    – Sabrina T.
    Commented Aug 27, 2021 at 11:54
  • @SabrinaT. It's in the resetUI method demonstrated above (called after the record loads).
    – sfdcfox
    Commented Aug 27, 2021 at 12:00
  • I understand that now I controll it using aura:attribute "showOnshoreWeight". I've tried to substitute default="{!false}" to include my condition, but it works as my first code.
    – Sabrina T.
    Commented Aug 27, 2021 at 12:03
  • @SabrinaT. The default value doesn't really matter; you need to check the record's value, which isn't available until the recordUpdated event fires.
    – sfdcfox
    Commented Aug 27, 2021 at 12:42

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .