6

I'm using lightning:recordEditForm and lightning:inputField to update a record. I need to validate the values before updation and I couldn't find any ways to get the values from lightning:recordEditForm or lightning:inputField. How can I access the values from lightning:recordEditForm and lightning:inputField? Is it possible ? If not why was it not allowed ?

Component:

<aura:component >

    <aura:attribute name="fieldList" type="String[]" default="['Name','Email','Birthdate','Languages__c','MobilePhone']"/>

    <lightning:recordEditForm recordId="003XXXXXXXXXXXXXXX" objectApiName="Contact" onsubmit="{!c.submitData}">
        <lightning:messages />
        <aura:iteration items="{!v.fieldList}" var="field">
            <lightning:inputField aura:id="inputs" fieldName="{!field}" />
        </aura:iteration>
        <lightning:button class="slds-m-top_small" variant="brand" type="submit" name="update" label="Update" />
    </lightning:recordEditForm>

</aura:component>

Controller:

({
    submitData : function(component, event, helper) {
        var values = component.find('inputs');
        console.log('values',values);
        //Can't get the values.
    }
})
  • i am also stuck with same isue.. i have to validate a field based on value in another field. i do not want to go with validation rule and do this in component level itself. atleast required attribute should be good. – Ravi Narayanan Feb 7 '18 at 18:04
5

There is a way to bind the lighthning:inputField value to an Instance of an Object. Here is an Example code,

Parent Component:

<aura:component >

    <aura:attribute name="testContact" type="Contact" access="PUBLIC" default="{'sObjectType':'Contact'}"/>

    <div class="slds-box">
        <lightning:recordEditForm aura:id="recordEdit" objectApiName="Contact">
            <aura:iteration items="['LastName','Phone']" var="item">
                <c:inputFieldCmp aura:id="child" testContact="{!v.testContact}" fieldName="{!item}"/>
            </aura:iteration>
        </lightning:recordEditForm>
    </div> 

    <div  class="slds-m-around_medium">
        <lightning:button label="Save" variant="brand" onclick="{!c.recordSave}"/>
    </div>
</aura:component>

Child Component:

<aura:component >
    <aura:attribute name="testContact" type="Contact"/>
    <aura:attribute name="fieldName" type="String"/>

    <lightning:inputField aura:id='inputField' fieldName="{!v.fieldName}" onchange="{!c.change}"/>
</aura:component>

Child Component Controller:

({
    change: function(cmp,event,helper){

        cmp.get('v.testContact')[cmp.get("v.fieldName")] =  event.getSource().get('v.value');

    }
})

Parent Component Controller:

({
    recordSave : function(component, event, helper) {
        console.log(JSON.stringify(component.get('v.testContact')));
    }
 })

On this Way you can simply bind the values to an Object Instance and Validate. Also you can make it possible with giving recordId attribute to the lighthing:recordEditForm by just make the record as that testContact attribute.

  • @am_Aravi " you can make it possible with giving recordId attribute to the lighthing:recordEditForm by just make the record as that testContact attribute." can you explain how exactly? it works like the example you have shown, but i cant understand how to specify an id – Dana Griff Oct 28 '18 at 13:05

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.