# Data validation

Use declarative rules to check required values, numeric ranges, string formats, and custom conditions. The default mode may only mark invalid values, so explicitly set invalidEditMode to block saving when required.

Canonical: https://grid.syidax.com/docs/features/validation-rules/

<a id="understand"></a>

## How it works and when to use it

Empty values may pass rules other than required. For mandatory quantities, combine required with numeric rules.

In the code, grid is the Grid element prepared in the installation chapter. Check each example's data and settings, and implement the app functions and server endpoints for your project.

- [Installation and integration · Shared sample data](https://grid.syidax.com/docs/guides/getting-started.md)

<a id="example-1"></a>

## Example 1. Required nonnegative integer quantity

Restrict empty values, decimals, and negative numbers separately.

```javascript
grid.validationRules = [
  { field: 'quantity', type: 'required', message: 'Enter a quantity.' },
  { field: 'quantity', type: 'integer', message: 'Enter a whole number.' },
  { field: 'quantity', type: 'min', value: 0, message: 'Must be at least 0.' }
];
grid.invalidEditMode = 'block';
```

Verify the result: Block commits of invalid input. A browser number editor's min restriction and business validation may be different stages.

<a id="example-2"></a>

## Example 2. Summarize errors before saving

Show the full table's error list in your app's review area.

```javascript
const errors = await grid.getValidationErrors();
console.log(errors);
if (errors.length) console.warn('Some cells still need correction.');
```

Verify the result: Read row IDs, columns, values, and error messages. Visible red indicators alone do not describe validation across the full data set.

<a id="example-3"></a>

## Example 3. Track failed input

Explain validation rejections and verify that the server also rejects the same values.

```javascript
grid.addEventListener('cellValidationFailed', event => console.log(event.detail));
```

Verify the result: Identify which input in which cell caused the problem. Avoid exposing sensitive values when recording user input to reproduce errors.

<a id="pitfalls"></a>

## Combining features and troubleshooting

Clearly choose an allow, block, or revert policy. allow applies the value while showing an error, so invalid values can remain in the data.

Distinguish invalid source data from errors in new user input. The server save API must enforce business rules too.

<a id="related-api"></a>

## APIs used by this feature

- [validationRules](https://grid.syidax.com/docs/options/validationRules.md) — Defines required values, ranges, lists, patterns, custom validation, and other input checks.
- [invalidEditMode](https://grid.syidax.com/docs/options/invalidEditMode.md) — Selects allow to apply invalid values with errors, revert to restore previous values, or block to prevent commits.
- [getValidationErrors](https://grid.syidax.com/docs/methods/getValidationErrors.md) — Reads row, field, value, and message information for current validation errors. Connect to save guidance or an error list.
- [cellValidationFailed](https://grid.syidax.com/docs/events/cellValidationFailed.md) — Emitted when validation fails. Use the target cell, input, and message to guide corrections.

<a id="next"></a>

## Related reading

- [How rows, columns, and IDs relate](https://grid.syidax.com/docs/guides/concepts.md)
- [Connect results and events](https://grid.syidax.com/docs/guides/data-event-access.md)
- [Explore in Features](https://grid.syidax.com/grid/validation-rules) — The guide describes which interactions are available to try.
