# Pre-Edit Validation

Canonical page: https://www.adaptabletools.com/docs/handbook-validating-pre-edit

- AdapTable allows developers to specify which columns / cells in AG Grid are editable and which are read-only
- Both editable and read-only cells can be specifically styled for easy identification

AdapTable provides Pre-Edit validation - which prevents read-only cells from being edited.

- Read-only and editable cell evaluation is performed on the **current** cell value, i.e. before any cell editing happens
- This is different to [Client Validation](https://www.adaptabletools.com/docs/handbook-validating-client/index.md) and [Server Validation](https://www.adaptabletools.com/docs/handbook-validating-server/index.md) which both evaluate the **proposed** new cell value

AdapTable fully respects as read-only any Column Definitions configured with `editable` as `false` in AG Grid GridOptions (including if editability is set via a function).

But sometimes a more granular or dynamic approach is required, where the editability of each cell in the same column is dependent on its value (or other data in the same row).

Although AdapTable respects editability set in an AG Grid column definition, we advise using the function below instead

## Setting Editable Cells

The `isCellEditable` function in [Edit Options](https://www.adaptabletools.com/docs/technical-reference-editing/index.md) enables editability evaluation to be made on a per-cell basis.

### `isCellEditable`

Function which sets if a cell is editable

Function which sets whether any given Cell in AG Grid is editable or read-only.

If this function is provided AdapTable will invoke it before editing is allowed to happen.

- The `isCellEditable()` function guards only against any **user** (i.e. UI) driven edits;
- Programmatic edits (e.g. via 'ticking' data) are still possible

The function is defined as follows:

```ts
isCellEditable?: (cellEditableContext: CellEditableContext) => boolean;
```

The [`CellEditableContext`](https://www.adaptabletools.com/docs/reference/celleditablecontext.md) object is defined as follows:

| Property | Type | Description |
| --- | --- | --- |
| [defaultColDefEditableValue](https://www.adaptabletools.com/docs/reference/celleditablecontext.md#defaultcoldefeditablevalue) | `boolean` | Default editability according to the AG Grid Column Definition (`ColDef.editable`) |
| [gridCell](https://www.adaptabletools.com/docs/reference/celleditablecontext.md#gridcell) | [`GridCell`](https://www.adaptabletools.com/docs/reference/gridcell.md)`<TData>` | Cell being edited |
| [adaptableContext](https://www.adaptabletools.com/docs/reference/celleditablecontext.md#adaptablecontext) | `any` | Custom application Context provided in `AdaptableOptions.adaptableContext` |

As can be seen it includes a very useful `defaultColDefEditableValue` property. This allows you to return the default editability for the Cell (using the `ColDef.editable` property in AG Grid `GridOptions`)

The [`GridCell`](https://www.adaptabletools.com/docs/reference/gridcell.md) object provides full details of the current cell, column and AG Row Node:

| Property | Type | Description |
| --- | --- | --- |
| [column](https://www.adaptabletools.com/docs/reference/gridcell.md#column) | [`AdaptableColumn`](https://www.adaptabletools.com/docs/reference/adaptablecolumn.md)`<TData>` | Column in which Cell is situated |
| [displayValue](https://www.adaptabletools.com/docs/reference/gridcell.md#displayvalue) | `any` | Display value of Cell (e.g. if formatted) |
| [isPivotCell](https://www.adaptabletools.com/docs/reference/gridcell.md#ispivotcell) | `boolean` | Is Cell in a Pivot Column |
| [isRowGroupCell](https://www.adaptabletools.com/docs/reference/gridcell.md#isrowgroupcell) | `boolean` | Is Cell in a Row Group Column |
| [normalisedValue](https://www.adaptabletools.com/docs/reference/gridcell.md#normalisedvalue) | `any` | Normalised value of Cell |
| [primaryKeyValue](https://www.adaptabletools.com/docs/reference/gridcell.md#primarykeyvalue) | `any` | Primary Key column&#39;s value in row - how Adaptable locates the Cell |
| [rawValue](https://www.adaptabletools.com/docs/reference/gridcell.md#rawvalue) | `any` | Raw value of Cell |
| [rowNode](https://www.adaptabletools.com/docs/reference/gridcell.md#rownode) | `IRowNode<TData>` | AG Grid Row Node that holds the Cell |
| [visible](https://www.adaptabletools.com/docs/reference/gridcell.md#visible) | `boolean` | Is Cell in a currently filtered Row |

```ts {2}
editOptions: {
  isCellEditable: (context: CellEditableContext) => {
    // Only GitHub Stars under 1000 are editable
    if (context.gridCell.column.columnId == 'github_stars') {
      return context.gridCell.rawValue < 1000;
    }
    // Only TypeScript and JavaScript languages are editable
    if (context.gridCell.column.columnId == 'language') {
      return (
        context.gridCell.displayValue === 'JavaScript' ||
        context.gridCell.displayValue === 'TypeScript'
      );
    }
    // for all other Columns and Cells return column default editability
    return context.defaultColDefEditableValue;
  },
},
```

This **takes precedence** over the `colDef.editable` property in GridOptions

See the Developer Tutorial on [Setting Cell Editability](https://www.adaptabletools.com/docs/dev-guide-tutorial-setting-cell-editability/index.md) for more details on how AdapTable evaluates cell editability

## Styling Editable / ReadOnly Cells

When using the [isCellEditable](https://www.adaptabletools.com/docs/technical-reference-editing/index.md) function, you might wish also to visually indicate to your users which Cells in AG Grid are editable.

Use `editableCellStyle` and `readOnlyCellStyle` in [User Interface Options](https://www.adaptabletools.com/docs/technical-reference-ui/index.md) to achieve this.

See [Styling Editable Cells](https://www.adaptabletools.com/docs/handbook-editing-cell-styling/index.md) and the [UI Guide for ReadOnly, Editable and Edited Cell Styles](https://www.adaptabletools.com/docs/ui-tutorial-editable-styles/index.md) for examples

**Example: Editable Cells**

Setting particular cells in AdapTable to be editable

- Three read-only rules in `adaptableOptions.ts` via `isCellEditable` in [Edit Options](https://www.adaptabletools.com/docs/technical-reference-editing/index.md):
  - No cell in any row where `language` is _HTML_ is editable
  - `github_stars` is not editable when the value is over 20,000
  - `name` is not editable where `week_issue_change` is negative
- `readOnlyCellStyle` in [User Interface Options](https://www.adaptabletools.com/docs/technical-reference-ui/index.md) adds a brown border and radius on read-only cells

Demo project (Vanilla); the files without a marker are shown below:

```text
src/
  adaptableOptions.ts
  agGridModules.ts     [template]
  columnDefs.ts        [template]
  gridOptions.ts       [template]
  index.ts             [template]
  onAdaptableReady.ts  [template]
  rowData.ts           [template]
  styles.css           [template]
index.html             [template]
package.json           [template]
tsconfig.json          [template]
```

Files marked `[template]` are the same in every demo; see the [demo template](https://www.adaptabletools.com/llms/demo-template.md#vanilla).

**`src/adaptableOptions.ts`**

```ts
import {
  AdaptableOptions,
  CellEditableContext,
  GridCell,
} from '@adaptabletools/adaptable';
import {WebFramework} from './rowData';

export const adaptableOptions: AdaptableOptions<WebFramework> = {
  primaryKey: 'id',
  adaptableId: 'Editable Cells',
  editOptions: {
    isCellEditable: (cellEditableContext: CellEditableContext) => {
      const gridCell: GridCell = cellEditableContext.gridCell;
      if (!gridCell || !gridCell.column) {
        console.log('no grid cell...');
        return true;
      }
      // No row where 'Language' is 'HTML'
      if (gridCell.rowNode.data['language'] === 'HTML') {
        return false;
      }

      // GitHub Stars not editable if > 20000
      if (gridCell.column.columnId === 'github_stars') {
        if (gridCell.rawValue > 20000) {
          return false;
        }
      }

      // Name not editable where Issue Change is negative
      if (
        gridCell.column.columnId === 'name' &&
        gridCell.rowNode.data['week_issue_change'] < 0
      ) {
        return false;
      }
      return cellEditableContext.defaultColDefEditableValue;
    },
  },
  userInterfaceOptions: {
    readOnlyCellStyle: {
      BorderColor: 'var(--ab-color-palette-17)',
      BorderRadius: 6,
    },
  },
  initialState: {
    Theme: {CurrentTheme: 'dark'},
    Layout: {
      CurrentLayout: 'Standard Layout',
      Layouts: [
        {
          Name: 'Standard Layout',
          TableColumns: [
            'name',
            'language',
            'github_stars',
            'week_issue_change',
            'github_watchers',
            'created_at',
            'has_wiki',
            'updated_at',
            'pushed_at',
            'open_issues_count',
            'open_pr_count',
            'closed_pr_count',
            'has_projects',
            'has_pages',
          ],
          AutoSizeColumns: true,
        },
      ],
    },
  },
};
```
