function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Mishu BhatMishu Bhat 

Hide a field based on a picklist value in lightning component

Hi,

I want to show the 'SuppliedEmail' field on the form when the picklist value of 'Investigation_Email_Preference__c' is either "abc" or "def".

How do i do that with lightning:recordForm ?

Component:
<aura:component implements="flexipage:availableForAllPageTypes,force:lightningQuickActionWithoutHeader,force:hasRecordId">
<aura:attribute name="myFields" type="List" default="['Case_Investigation__c','Investigation_Subject__c','Investigation_Email_Preference__c','Tier2_Agent__c','SuppliedEmail']"/>
<lightning:recordForm
        recordId="{!v.recordId}"
        objectApiName="Case"       
        columns="2"
        fields="{!v.myFields}"
        mode="edit"
        onsubmit="{!c.handleSubmit}"/>

</aura:component>
Raj VakatiRaj Vakati
While you can certainly show and hide fields using JavaScript, it's usually more idiomatic in Lightning to allow the framework to handle conditional rendering using <aura:if> expressions built around the values of your attributes.
Here's a quick example of how to show and hide fields idiomatically in a Lightning component. This is an abbreviated example that's meant to show the structure, not a working component! I'd encourage adapting this type of structure to your specific needs

 
<aura:component>
    <aura:attribute name="industry" type="String" default="Education" />
    <aura:attribute name="otherIndustry" type="String" />

    <lightning:select name="select1" label="What industry?" value="{! v.industry }">
        <option value="Education">Education</option>
        <option value="Finance">Financial Services</option>
        <option value="Sports">Sports and Recreation</option>
        <option value="Other">Other</option>
    </lightning:select>

    <aura:if isTrue="{! v.industry == 'Other' }">
        <lightning:input label="Other Industry" value="{! v.otherIndustry }" />
    </aura:if>
</aura:component>

 
Mishu BhatMishu Bhat
I want the values and datatype of the fields to be picked up from the custom object. For eg, the Investigation_Email_Preference__c is a picklist field and all the values are taken from the custom object. thats why i used the lightning Record form.

Can you show me how can i incorporate showing/hiding a field with the lightning record form?
Nitisha TumbleNitisha Tumble
Hi Mishu,
Did you find a way to hide/show custom fields based on picklist field values?
suji srinivasansuji srinivasan
Hi Nitisha ,Refer this https://developer.salesforce.com/forums/?id=9062I000000Qv1oQAC