0

I have a lightning component with two input fields. But the values enter in the input form doesn't bind and record is created with empty values.

Component

<aura:attribute name="sites" type="Site_Registration__c[]" />
<aura:attribute name="newSite" type="Site_Registration__c" access="global" default="{'sobjectType':'Site_Registration__c',
                                                                                     'Name':'Test',
                                                                                     'Company_Name__c':'',
                                                                                     'Project_Name__c':''
                                                                                     }" />

<input aura:id="companyName" placeholder="Company Name" class="slds-input" type="text" value="{!v.newSite.Company_Name__c}" required="true"/>

<input aura:id="projectName" placeholder="Project Name" class="slds-input" type="text" value="{!v.newSite.Project_Name__c}" required="true"/>

<lightning:button variant="brand" label="Submit" title="Submit" onclick="{!c.submitSite}" />

Controller JS

submitSite : function(component, event, helper) {

    var newSiteReg = component.get("v.newSite");         

    console.log("Site Info Controller " + JSON.stringify(newSiteReg));

    helper.insertSiteRegHelper(component,newSiteReg);
},

Helper JS

({
    insertSiteRegHelper : function(component,siteReg) {

     var action= component.get("c.populateSiteReg");

        action.setParams({"siteReg" : siteReg
                      });
     action.setCallback(this, function(response)
     {
        var state = response.getState();
        if (state === "SUCCESS") 
        {

            var sites = component.get("v.sites");
            sites.push(response.getReturnValue());
            component.set("v.sites", sites);
        }
        else
        {
        }    
     });
     $A.enqueueAction(action);  
    },
})

Apex Controller

   @AuraEnabled
    public static void populateSiteReg(Site_Registration__c siteReg)
    {

        if(siteReg != null){
            Database.insert(siteReg);
        }
    }

Any values entered to the Company Name and Project Name fields are not getting save to the record and record is creating with only value in the Name field. Any idea what is wrong here? Your comments are greatly appreciated as I'm stuck here for few days. Thanks...!

0

Its because you are using HTML tag. So you need to manually get the data from Lightning and pass it in the controller. It's quite similar with VF where if we used HTML tag then we need to pass that data manually in controller.

You can use Lightning tag in that you will get the data and you don't need to worry about this. Here is a sample for you.

<lightning:input name="input8" value="{! v.firstName }" placeholder="type your first name..." label="Text field with attribute binding" />
  • You are correct. After changing to <lightning:input /> tags, my problem solved. Thank you so much for the answer. – Sudh May 27 '19 at 3:47
  • @Sudh if it solved your problem then you can accept this answer and close this question. – Tushar Sharma May 27 '19 at 5:04
  • Accepted your answer. Thanks again. – Sudh May 27 '19 at 6:15

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.