Lightning Aura Components – How to Check for Empty Fields

<aura:attribute name="guid" type="String" default="{!v.simpleRecord.Loan_Guid__c}"/>
    <aura:attribute name="fileCreated" type="Boolean" default="{!v.guid != '' ? true : false}"/>
        guid--{!v.guid}--

I have this bit of code which is supposed to set the "fileCreated" boolean based on where or not the object has a guid. the v.guid is getting set correctly but for some reason my conditional doesn't seem to recognize an empty value.

!v.guid is never equal to '' even when the field is blank. If i do something like v.guid == "hello" and set the field to hello it will recognize that string but it cannot recognize an empty field. In the bi tabove wher ei print the guid guid–{!v.guid}–, it will print the guid and print guid— when the guid is empty.

Best Answer

Use empty to determine if something is empty (or, in this case, not empty):

{!not(empty(v.guid))}

However, I'm not sure you can do all this with the "default" attribute, since it's not meant to be used this way. The documentation says:

The default value for the attribute, which can be overwritten as needed. When setting a default value, expressions using the $Label, $Locale, and $Browser global value providers are supported. Alternatively, to set a dynamic default, use an init event. See Invoking Actions on Component Initialization.

If you choose do to what you're doing, you're venturing in to undefined-behavior territory.

Related Topic