0

I am using Lightning component and in that I have a Recordeditform now on submit I check for the validations of fields and if there needs to be some error then I use event.preventDefault() and set the error message which is working fine but the form shows an error but in the backend if I check that record that record is already being updated with the latest values used in the form. I don't want to save the data of records until I don't submit the form again.

Component:
<lightning:recordEditForm objectApiName="Object__c"
                                              onsuccess="{!c.handleSuccess}"
                                              onsubmit="{!c.handleSubmit}"
                                              onerror="{!c.handleError}"
                                              recordId="{!v.record.id}"
                                              aura:id="editEnd">
                        <lightning:messages aura:id="errorMsg"/>
                        <lightning:outputField fieldName="abc__c"/>
                        <lightning:inputField fieldName="xyz__c" value="{!v.record.xyz__c}" required="true"/>
                        <lightning:inputField fieldName="change__c" required="true"/>
                        <lightning:layout horizontalAlign="center" class="slds-m-top_large">
                            <lightning:button variant="neutral" label="Cancel" title="Cancel" type="text" onclick="{!c.closeModel}"/>
                            <lightning:button variant="brand" label="Submit" title="Submit" type="submit"/>
                        </lightning:layout>
                    </lightning:recordEditForm>

Controller:
handleSubmit: function (component, event, helper){
    component.find("sSpinner").set("v.class" , 'slds-show');
    event.preventDefault();
    var record = component.get('v.record'); 
    var eventFields = event.getParam("response").fields; //get the fields 
    if(record.end != eventFields["xyz__c"].value){ 
        component.find("sSpinner").set("v.class" , 'slds-hide'); 
        var validationFail = "change field is mandatory."
        component.find("errorMsg").setError(validationFail);
        event.preventDefault();

    } else { 
        component.find('editEnd').submit(eventFields);
    } 
},

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.