1

I have a lightning data table displaying a set of Records. Also, it have inline editing set true for one column called Price.

Is it possible to have the Price column as Required while editing?

My Cmp code:

<lightning-datatable
          data-id={table.category}
          key-field="Id"
          selected-rows={preSelectedRows}
          onsave={handleSave}
          draft-values={draftValues}
          max-row-selection =1
          data={table.data}
          columns={column}
          
        >

MY js:

 column = [
        { fieldName: 'Name', label: 'Name' },
        { fieldName: 'Description__c', label: 'Description' },
        {fieldName:'Place__c',label:'Place'},
        {fieldName:'Price__c',label:'Price',type:'currency',editable:true}

    ];

How to make price field as a required field in this table ?

1
  • If you can't set it as required field in the object, then you will have to handle it via code. I doubt if there is an OOB attribute by which you can mark it as required.
    – arut
    Apr 23 at 17:27
4

There are a few scenarios you should consider when requiring / validating entry in a lightning-datatable.

Example Required Field in LWC Lightning Datatable

A custom "required input cell" with an asterisk on edit would only be possible if you create a custom data type, see the following resources:

OOTB - Required Field

If the field is configured, out of the box (OOTB), as required, then an error on the cell will display after you attempt a save. The DML operation will fail, and the standard table error will be returned: LWC Datatable: See section 'Displaying Errors'

OOTB - Apex (Triggers) / Validation Rules

When an error is added to a record during a DML operation, via configuration or Apex code, then those errors will propagate back to the LWC datatable as error messages at the table and row levels -- see above documentation 'Displaying Errors'.

Client side validation

If none of the above are configured in your org, then you can create an onsave handler in your JavaScript, check the validity of the table rows (both draftValues & existing table data), and if validation fails populate an error object that you pass back to the table error attribute. See above documentation 'Working with Inline Editing'


From a UI perspective you can always provide additional clarity on the required column.

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.