# Client Data Validation

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

- Client-side validation in AdapTable can be used to prevent invalid Edits from completing
- They are essentially a special type of Alert

AdapTable provides Client Data Validation via Alerts (defined in [Alert State](https://www.adaptabletools.com/docs/technical-reference-alerting/index.md))

AdapTable also offers [Server Validation](https://www.adaptabletools.com/docs/handbook-validating-server/index.md) where the validation is handled externally

These Data Validation rules prevent cell edits **before** the new value is sent to the server.

- Ultimately, these are Alerts with `PreventEdit` set to _true_ in the Alert definition.
- If the Alert Rule is broken, AdapTable will undo the cell edit and fire the Alert.

This avoids the irritating scenario where an edit is made and committed which all other users of the application see before server validation forces the cell to 'jump back' to its initial value.

- This is not the same as [Pre-Edit Validation](https://www.adaptabletools.com/docs/handbook-validating-pre-edit/index.md) which prevents **all** edits to read-only cells
- Client data validation is for the use case where a cell is editable but not all edits are necessarily valid

**Example: Client Data Validation**

Performing Client Data Validation with a Prevent Edit Alert

- In this (slightly contrived) example, we have prevented cells in the `language` column being changed to _Python_ (see `adaptableOptions.ts`).
- The Alert displays a [Toast Notification](https://www.adaptabletools.com/docs/ui-tutorial-using-toast-notifications/index.md) with a custom message and type `Error` at the top of the grid.

- Change a cell in the `language` column to `Python` and see how it triggers a Prevent Edit Alert
- Or go a stage further and create an Alert with a custom form 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.

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: 'Client Data Validation',
  initialState: {
    Dashboard: {
      ModuleButtons: ['Alert'],
      Tabs: [
        {
          Name: 'Demo',
          Toolbars: ['Alert'],
        },
      ],
    },
    Theme: {CurrentTheme: 'dark'},
    Alert: {
      AlertDefinitions: [
        {
          Name: 'alert-language',
          Scope: {
            ColumnIds: ['language'],
          },
          Rule: '[language] = "Python"',
          AlertProperties: {
            PreventEdit: true,
            DisplayNotification: true,
          },
          MessageType: 'Error',
          MessageText: 'You cannot change the Language to "Python"!',
        },
      ],
    },
    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,
        },
      ],
    },
  },
  notificationsOptions: {
    position: 'TopCenter',
  },
};
```

- See [Validation Alerts](https://www.adaptabletools.com/docs/handbook-alerting-validation/index.md) for more details
- Alerts with Data Validation Rules are defined in [Alert Initial State](https://www.adaptabletools.com/docs/technical-reference-alerting/index.md)

### FAQ

**Why add Prevent Edit Alerts which perform validation on the client - surely it should take place on the server?**
That is correct, validation should ideally take place on the server and hopefully it does for our users.
And AdapTable offers [Server Validation](https://www.adaptabletools.com/docs/handbook-validating-server/index.md) for this reason.
Instead _Prevent Edit_ Alerts specifically deal with 3 common validation-related use cases:

1. add an extra level of validation so that you can prevent edits which are usually permitted, but which in particular scenarios or use cases should be avoided

2. avoid unnecessary round trips to the server, particularly if this has other knock-on consequences or affect other users that might see their screen flash first with the new value and then again with the old value

3. as a temporary measure before Server Validation has been added; like all AdapTable objects, Alert Definitions will come into effect immediately after that they are created in the UI - there is no down-time needed, nor any custom development required, and no systems need to be restarted
