1

As per below salesforce documentation we can display error for lightning datatable by setting the "error" attribute.

https://developer.salesforce.com/docs/component-library/bundle/lightning:datatable/documentation

triggerError: function (cmp, event, helper) {
    cmp.set('v.errors', {
        rows: {
            b: {
                title: 'We found 2 errors.',
                messages: [
                    'Enter a valid amount.',
                    'Verify the email address and try again.'
                ],
                fieldNames: ['amount', 'contact']
            }
        },
        table: {
            title: 'Your entry cannot be saved. Fix the errors and try again.',
            messages: [
                'Row 2 amount must be number',
                'Row 2 email is invalid'
            ]
        }
    });
}

the above example is for showing one error. Since lightning:datatable has multi-edit functionality, a user can try to edit multiple rows at the same time.

is there any way to display the error for all the edited rows?

I tried to do something like below, but I am getting compilation error.

component.set('v.errors', {
                rows: [{
                    a: {
                        title: 'We found 2 errors.',
                        messages: [
                            'Enter a valid amount.',
                            'Verify the email address and try again.'
                        ],
                        fieldNames: ['estDebt', 'responsibility']
                    }
                },
                table: {
                    title: 'Your entry cannot be saved. Fix the errors and try again.',
                    messages: [
                        'Row 2 amount must be number',
                        'Row 2 email is invalid'
                    ]
                },
                {
                    b: {
                        title: 'We found 2 errors.',
                        messages: [
                            'Enter a valid amount.',
                            'Verify the email address and try again.'
                        ],
                        fieldNames: ['estDebt', 'responsibility']
                    }
                },
                table: {
                    title: 'Your entry cannot be saved. Fix the errors and try again.',
                messages: [
                    'Row 2 amount must be number',
                    'Row 2 email is invalid'
                ]
            }]
        });
2

Your format is messed up. There should be two top-level objects, rows and table. The rows key should contain keys that match the keyField values for the table. Table messages themselves should be in the table key.

{ rows: 
  { a: { title: "...", messages: [...], fieldNames: [...] }, 
    b: { title: "...", messages: [...], fieldNames: [...] }
  },
  table: { title: "...", messages: [...] }
}

Assuming you had a list of errors from the server, you might do something like this:

var errors = { rows: {}, table: {} }
errorsFromServer.forEach(
  error => errors.rows[error.Id] = { title: "...", messages: [...], fieldNames: [...] }
);
errors.table.title = "We found "+errorsFromServer.length+" error(s). ...";
errors.table.messages = [...];
cmp.set("v.errors", errors);
| improve this answer | |
  • Thanks! it is helpful. By any chance can we use them with for loop? I can store the error attributes in List/Map in the controller, but I want to iterate the error attributes with for loop instead of hardcoding them. – SFDC Geek Jan 7 '19 at 19:28
  • @SFDCGeek Updated answer based on your comment. Does this help? – sfdcfox Jan 7 '19 at 19:42

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.