# Validation Alerts

Canonical page: https://www.adaptabletools.com/docs/handbook-alerting-validation

- Validation Alerts can prevent bad edits
- They will fire before the edit is committed, preventing a round trip to the server

Validation Alerts can be used to **prevent cell edits** which break the rule set in the [Alert Definition](https://www.adaptabletools.com/docs/technical-reference-alerting/index.md).

This is done by specifying `PreventEdit` in the `AlertProperties` property to _true_.

If a Prevent Edit Alert is too inflexible, an Alert with an `Undo` action can be used instead

Validation Alerts are designed to avoid this common, and extremely annoying, scenario:

1. an edit is made and commmitted
2. all other users of the application see this edit
3. server validation then kicks in and forces the cell to 'jump back' to its initial value in all users' grids

- An alternative to validation Alerts is to create an Alert with a [custom form](https://www.adaptabletools.com/docs/handbook-alerting-notifications/index.md) enabling users to provide a new value
- This can be achieved via an [Adaptable Form](https://www.adaptabletools.com/docs/ui-tutorial-configuring-adaptable-forms/index.md) with 2 [Buttons](https://www.adaptabletools.com/docs/ui-tutorial-displaying-adaptable-buttons/index.md) that reference [Grid API](https://www.adaptabletools.com/docs/handbook-managing-grid-data/index.md) methods when clicked

**Example: Alerts: Validation Alerts**

Alerts which prevent cell edits

- This example contains a Validation Alert which prevents a cell edit which sets `Github Stars` to 0
- It is of type `Error` and displays a Notification when the Alert fires

- Set a `GitHub Stars` value to 0 and see the change being reverted and the Alert notification displayed

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} from '@adaptabletools/adaptable';
import {WebFramework} from './rowData';

export const adaptableOptions: AdaptableOptions<WebFramework> = {
  primaryKey: 'id',
  adaptableId: 'Alert Prevent Cell Edit',
  notificationsOptions: {
    position: 'TopCenter',
  },

  initialState: {
    Dashboard: {
      ModuleButtons: ['Alert'],
      Tabs: [
        {
          Name: 'Toolbars',
          Toolbars: ['Alert'],
        },
      ],
    },
    StatusBar: {
      StatusBars: [
        {
          Key: 'Center Panel',
          StatusBarPanels: ['Alert'],
        },
      ],
    },
    Theme: {CurrentTheme: 'dark'},
    Alert: {
      AlertDefinitions: [
        {
          Name: 'alert-github-stars',
          MessageType: 'Error',
          Scope: {
            ColumnIds: ['github_stars'],
          },
          Rule: '[github_stars] = 0',
          AlertProperties: {
            PreventEdit: true,
            DisplayNotification: true,
          },
        },
      ],
    },
    Layout: {
      CurrentLayout: 'Standard Layout',
      Layouts: [
        {
          TableColumns: [
            'name',
            'language',
            'github_stars',
            'license',
            'has_projects',
            'has_pages',
            'created_at',
            'has_wiki',
            'updated_at',
            'pushed_at',
            'github_watchers',
            'week_issue_change',
          ],
          Name: 'Standard Layout',
          AutoSizeColumns: true,
        },
      ],
    },
  },
};
```

### Providing Validation Alerts in Initial State

Same shape as a [Data Change Alert](https://www.adaptabletools.com/docs/handbook-alerting-data-change/index.md) — `Scope` plus a boolean `Rule` evaluated on the **proposed** cell value.

Set `AlertProperties.PreventEdit` to *true* to roll back the edit when the rule is true. Use `DisplayNotification` so users see why the edit was rejected.

```ts [[1, 2, "Alert"], [1, 3, "AlertDefinitions"], [1, 9, "Scope"], [1, 10, "ColumnIds"], [1, 12, "Rule"], [2, 13, "AlertProperties"], [2, 14, "PreventEdit"], [1, 21, "Scope"], [1, 24, "Rule"], [2, 25, "AlertProperties"], [2, 26, "PreventEdit"], [2, 28, "HighlightCell"]]
const initialState: InitialState = {
  Alert: {
    AlertDefinitions: [
      {
        Name: 'alert-no-zero-stars',
        MessageType: 'Error',
        MessageHeader: 'Invalid value',
        MessageText: 'GitHub Stars cannot be zero',
        Scope: {
          ColumnIds: ['github_stars'],
        },
        Rule: '[github_stars] = 0',
        AlertProperties: {
          PreventEdit: true,
          DisplayNotification: true,
        },
      },
      {
        Name: 'alert-no-negative-stars',
        MessageType: 'Error',
        Scope: {
          ColumnIds: ['github_stars'],
        },
        Rule: '[github_stars] < 0',
        AlertProperties: {
          PreventEdit: true,
          DisplayNotification: true,
          HighlightCell: true,
        },
      },
    ],
  },
};
```

### FAQ

**How do Validation Alerts differ from ReadOnly Cells**
The main difference is when the evaluation is performed:

- Use [ReadOnly cells](https://www.adaptabletools.com/docs/handbook-validating-pre-edit/index.md) ( via `isEditable` property in [Edit Options](https://www.adaptabletools.com/docs/technical-reference-editing/index.md)) to validate **before** the edit (using the initial cell value)
- Validation Alerts are evaluated **after** the proposed edit using the new cell value
