+ Start a Discussion
Basant Kr VermaBasant Kr Verma 

Not able to set value for Lightning:inputField field if it's manually modified from UI

Hi All,

I am facing issue with  Lightning:inputField with lightning:recordEditForm that when I am trying to programatically change the value of any field I am able to do only if user is not modifying that field manully on screen

What I mean is that, lets say I have a form using lightning:recordEditForm with only two fields of account which are Name and Type.
And I want a logic to introduce that, I want to add selected Type as Prefix on the Name field, I have created a on change method and doing it in that, and that is working as expected if user only changing the Type field
BUT if user first edit the Name field then select/change the Type its not.


Below is the my sample code
CustomRecordEditForm.cmp
<aura:component implements="flexipage:availableForAllPageTypes,forceCommunity:availableForAllPageTypes,force:lightningQuickAction,force:hasRecordId" access="global">
    <aura:attribute name="recordId" type="String"/>
    <div class="slds-container-outer">
        <div class="slds-container">
            <lightning:recordEditForm aura:id="recordViewForm" objectApiName="Account" recordId="{!v.recordId}">
                <lightning:messages />
                <lightning:inputField fieldName="Name" aura:id="Name" />
                <lightning:inputField fieldName="Type" aura:id="Type" onchange="{!c.handleTypeChange}"/>
            </lightning:recordEditForm>
        </div>
    </div>
</aura:component>

CustomRecordEditFormController.js
({
	handleTypeChange : function(component, event, helper) {
		var typeValue = component.find("Type").get("v.value");
        var nameValue = component.find("Name").get("v.value");
        if(typeValue != null && typeValue != '' && typeValue != undefined){
            if(nameValue.indexOf(":") > 0){
                nameValue = nameValue.split(':')[1];
            }
            component.find("Name").set("v.value",typeValue+":"+nameValue);
        }
	}
})

Thanks in Advance!
Basant Kr VermaBasant Kr Verma
Found one similure post on stackexchange which was asking to set "fieldName" also, tried that but it's still nt working with that.

Updated CustomRecordEditFormController.js cod is as below
({
	handleTypeChange : function(component, event, helper) {
		var typeValue = component.find("Type").get("v.value");
        var nameValue = component.find("Name").get("v.value");
        if(typeValue != null && typeValue != '' && typeValue != undefined){
            if(nameValue.indexOf(":") > 0){
                nameValue = nameValue.split(':')[1];
            }
            component.find("Name").set("v.fieldName","Name");
            component.find("Name").set("v.value",typeValue+":"+nameValue);
        }
	}
})

 
Basant Kr VermaBasant Kr Verma
stackexchange post link

https://salesforce.stackexchange.com/questions/207346/spring-18-value-attribute-in-lightninginputfield/227674#227674
Andrew Howell 15Andrew Howell 15
I agree - this is not flexible enough. You can modify the contents of the field during the "onload" event, and it will be respected, but not after that. This should be as simple as Salesforce adding a change handler to the value attribute. In my case, I need to re-format a phone number. I can re-format an existing number during the 'onload' event, but if I attempt to update the format after they've typed in 10 digits, the component simply ignores the request.