5

I have a component like this:

<div>
  <force:recordEdit aura:id="edit" recordId="{!v.contactId}" />
</div>
<div>
  <ui:button label="Save" press="{!c.saveEdit}"/>
  <ui:button label="Cancel" press="{!c.cancelEdit}"/>
</div>

I need to know how to handle validation errors in the controller. Since I'm using the force:recordEdit component, I don't know if I can gain access to the required fields and validate that they have a value before trying to save. The other option would be to do some kind of a try/catch on the save method - but I can't find any docs on how to do that exactly with the following code:

saveEdit : function(component, event, helper) {
    var edt = component.find("edit");
    console.log('edt: ',edt);
    edt.get("e.recordSave").fire();
    // Update the component
    helper.getContact(component);
    helper.toggleModal(component);
}

I either need to do some pre-validation on the form so I can assure that there are good values in each field, or I need to do some kind of try{edt.get("e.recordSave").fire();}catch(err){ do something here }

Anyone on here done this already?

  • Did you ever find an answer to this question or a workaround? – Daniel Flippance Sep 19 '17 at 17:49
2

You may want to bubble the event to the front end rather than catching it at backend .

Here is a simple patterns that I have been following for lightning components to show error on the page

component

 <aura:attribute name="messages" type="Aura.Component[]" access="GLOBAL"/>

<div aura:id="messages">
    {!v.messages}
</div>

Error handling in controller.js

if (state === "SUCCESS") {

}else if (state === "ERROR") {
   var errors = response.getError();
   if (errors[0] && errors[0].pageErrors) {
        console.log(errors[0].pageErrors);
        $A.createComponents([
            ["ui:message",{
                "title" : "Deletion Failed:",
                "severity" : "error",
            }],
            ["ui:outputText",{
                "value" : errors[0].pageErrors[0].message
            }]
            ],
            function(components) {
                var message = components[0];
                var outputText = components[1];
                // set the body of the ui:message to be the ui:outputText
                message.set("v.body", outputText);
                component.set("v.messages", message);  
                setTimeout(function() {
                $A.run(function() {
                    component.set("v.messages", []);                                       
                });
            }, 10000)
            } )   
        } else {
         $A.error("Unknown error");
        }
       }
   });
   $A.enqueueAction(action);
 }

Also i see one of my colleagues has a simpler way to handle the error .You may want to try that as well

https://balkishankachawa.wordpress.com/2016/01/19/understanding-server-side-errors-in-lightning/

  • 1
    I can see how this would work if you're doing a custom save action, and then using its callback to evaluate the response. However, I don't see that you can set a callback for the force:recordSave method? It's not referenced in the docs anywhere and I can't find any examples of it. – Jason Benkert Mar 4 '16 at 2:42
  • you can try and catch as you do in normal JS .Can you post that as a different question so that i can get attention from some SFDC folks – Mohith Shrivastava Mar 4 '16 at 3:40
2

I have the same question. I tried using the standard try/catch block to handle a validation error from the force:recordEdit component using the force:recordSave event, but my console returns nothing. I get to c.saveDeal, but no further.

Component.cmp

<aura:component controller="..." implements="..." access="public">

  <!-- Events -->
  <aura:registerEvent name="saveSuccess" type="force:recordSaveSuccess"></aura:registerEvent>
  <aura:handler name="onSaveSuccess" event="force:recordSaveSuccess" action="{!c.onSaveSuccess}"></aura:handler>

  <!-- MARKUP -->
  <ui:button label="Save" class="slds-button slds-button--neutral slds-button--small slds-button--brand" press="{!c.saveDeal}"></ui:button>
  <force:recordEdit aura:id="edit" recordId="{!v.recordId}"/>
  <!-- / MARKUP -->

</aura:component>

ComponentController.js

saveDeal : function(cmp) {
  console.log('in c.saveDeal');
  try {
    cmp.find("edit").get("e.recordSave").fire();
  }
  catch (e) {
    console.log(e);
  }
},
onSaveSuccess : function(cmp) {
  console.log('in c.onSaveSuccess');
  var recordId = cmp.get('v.recordId');
  $A.get("e.force:navigateToURL").setParams({
    "url": "/" + recordId,
    isredirect: true
  }).fire();
},
  • Could events with the force prefix be wrapped in a custom event so you can use a callback? – Isaac Apr 25 '16 at 21:13
  • This does not really answer the question. If you have a different question, you can ask it by clicking Ask Question. You can also add a bounty to draw more attention to this question once you have enough reputation. - From Review – Mohith Shrivastava Apr 25 '16 at 22:33
  • I was trying to clarify the question by providing feedback based on the proposed solution to this issue: 'you can try and catch as you do in normal JS' doesn't appear to work in a straightforward manner. I am new to StackExchange, so let me know how this feedback should be delivered if not here. – Isaac Apr 25 '16 at 22:41
  • Ah sorry about that .You need to have some more reputation so that you can comment on answers .Alright I am upvoting so you get enough reputations to get started .Thank you – Mohith Shrivastava Apr 25 '16 at 22:43
  • Ideally a comment is used to clarify, but this would be too big. The best option would be to edit the quesiton with some of this information... again you don't have the rep for that :) If someone could do that it'd be great, otherwise I'll circle back later when I've got more than 2 mins :) – Matt Lacey Apr 25 '16 at 23:28
0

I was figuring this out too, then I come up with this solution : Using the force:recordSaveSuccess.

At least with this even if i cannot capture the error directly, on success, I can execute an action. My context, is using the standard edit from a modal. But there were some validation rules on the object, so I should be able to capture the error, so that if there is an error, I do not close the modal. I manage to do it , but in the reverse order. That is, if there is not error, I execute the handleSaveSuccess method to close the modal, else, I do not close, and the validation rule message will be displayed as an standard functionality.

In my lightning controller I have these methods :

  save : function(component, event, helper) {
    console.log("## save implemented");
    //component.set("v.showSpinner",true);      
    component.find("edit").get("e.recordSave").fire();      
 },
 handleSaveSuccess : function(component, event) {
    component.set("v.hasChanged",component.get("v.hasChanged") + 1);        
    setTimeout(function(){
        component.set("v.closeMe",true); 
    }, 200);
}

Then in my component I have this :

 <aura:handler name="onSaveSuccess" event="force:recordSaveSuccess" action="{!c.handleSaveSuccess}"/>

Hope this help you all. while waiting for salesforce to give access to force:recordSaveError :)

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.