[SalesForce] Inline Editing field needs to be required field in Lightning Data table – LWC

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 ?

Best Answer

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.

Related Topic