Validation
Validate values with validate and the number editor's range, choose between marking and refusing the write (mark / reject), and check everything before saving
Write cell value checks on the column. Cells that fail get a mark and a message ('mark', the default), or the write itself is stopped ('reject'). To check everything before saving, use the handle's getInvalidCells() to go through all rows.
Demo (order entry > check everything on submit)
(Press Submit and the cells that fail show up here)
- No marks are shown while typing. Press "Submit" and the cells that fail get marks, the first one is selected, and the messages are listed below.
- Code is required, Qty is an integer of 1 or more (the number editor's
min/integer), and Due is after today (validate). Fix them and press "Submit" again to clear the marks. - Turn on "Show marks while typing too" and marks appear as you type, as by default.
Writing checks on columns
const columns: GridColumn<Order>[] = [
{
key: 'code', title: 'Code', width: 120,
validate: ({ value }) => String(value ?? '').trim() !== '' || 'Code is required',
validationMode: 'reject', // values that fail aren't written (default is 'mark')
},
{
key: 'qty', title: 'Qty', width: 100,
editor: { type: 'number', min: 1, integer: true }, // an integer of 1 or more (checked even without validate)
},
{
key: 'due', title: 'Due', width: 130, editor: { type: 'date' },
validate: ({ value, row }) =>
row.status === 'quote' || (typeof value === 'string' && value > today) || 'Set the due date after today',
},
];validatereturnstrue(passes) /false(fails, with the default message) / a string or{ message }(fails, with that message).- It receives
{ value, row, column }, whererowis the row before the write. Checks that combine values of other columns (Due above) can be written too. validateis called every time a visible cell is drawn, so keep it a light, pure function (don't call outside APIs).
Number ranges and integers
The number editor's min / max / integer: true work as value checks as they are.
- The order of checks is "is it a number → is it an integer → is it in range", and the first one that fails gives the message ("Enter a number", "Enter a whole number", "Enter 1 or more" and so on).
- Numbers and strings the column's default parser reads as numbers (
'12'/'1,234'/'12') count as numbers. Empty cells pass (usevalidateto make it required). - Written together with
validate,validatecomes first. Only values that passvalidatego through the range check. To change the message, write the same condition invalidateand return your own message. stepis only the increment for the ↑↓ keys and spinner; it isn't used for checks.
Numbers and dates that can't be read (strict)
The number and date editors put text they can't read as a number / date ("¥12,000", "2026年11月4日" and so on) in as text. With strict: true, values that can't be read get a mark (in 'reject' columns they aren't written. Since 1.2.0).
{ key: 'price', title: 'Unit price', width: 120, editable: true, editor: { type: 'number', strict: true } }, // "Enter a number"
{ key: 'due', title: 'Due', width: 140, editable: true, editor: { type: 'date', strict: true } }, // "Enter a date (e.g. 2026-03-15)"- Valid
Dates and strings the date default parser reads ('2026-03-15'/'2026/3/15'and so on) count as dates. Strings of digits only (Excel serial numbers such as "45678"), strings without a year ("11/2") and impossible dates ("2026/2/30") are values that can't be read. - With the number's
min/max/integer, "is it a number" is checked even withoutstrict. - Empty cells pass. Written together with
validate,validatecomes first.
What happens when a value fails (validationMode)
'mark' (default) | 'reject' | |
|---|---|---|
Committing in an editor (Enter / Tab) | Written, the cell is marked | Not committed; editing continues and an error bubble shows (cancelled when focus leaves) |
| Paste / fill handle | Written, the cell is marked | Only the failing cells are skipped (other cells are written) |
Clearing with Delete / Backspace | Written, the cell is marked | Skipped if empty fails (required columns can't be cleared) |
setValue in renderCell / toggling a checkbox | Written, the cell is marked | Nothing happens |
- The mark is the cell background and a small triangle at the top right. The message shows on hover and when the cell becomes the active cell with the keyboard / a tap.
- Marks are decided from the current value on every render, so after paste, undo or replacing
rowsfrom outside they always match the current data (the result isn't kept as state). - Change the colors with the tokens
--ssg-invalid/--ssg-invalid-bg, and the bubble's text color with--ssg-invalid-contrast. In Windows high contrast, a dotted border inside the cell is shown instead of the background. - Assistive technology gets
aria-invalidon failing cells and the message inaria-description. - When the same cell has a note (
cellNote), the error triangle overlaps the note triangle, and the messages appear in the order "error → note".
Deciding when marks show (showValidationMarks)
While showValidationMarks={false}, no marks are shown (and validate isn't called for visible cells). The checks stay written on the columns, and only when marks show is decided by state. Blocking writes in validationMode: 'reject' and the bubble at that time work regardless of this setting.
Checking everything before saving (getInvalidCells)
getInvalidCells() checks the checked columns × all rows only when called, and returns the list of failing cells { rowKey, sourceRowIndex, columnKey, message }[]. It checks even when marks aren't shown (showValidationMarks={false}), as well as hidden columns and rows hidden by filters.
const [showErrors, setShowErrors] = useState(false);
const submit = () => {
const grid = gridRef.current;
const invalid = grid?.getInvalidCells() ?? [];
if (invalid.length > 0) {
setShowErrors(true); // show the marks only now
// go to the first cell. Positions are view positions, so look them up again from row and column keys (skip rows hidden by filters)
const first = invalid.find((cell) => (grid?.getViewRowIndex(cell.rowKey) ?? -1) >= 0);
if (grid && first) {
const row = grid.getViewRowIndex(first.rowKey);
const col = grid.getColIndex(first.columnKey);
if (col >= 0) {
grid.selectCell(row, col, { scrollIntoView: true });
grid.focus(); // selectCell doesn't move focus
}
}
return;
}
setShowErrors(false);
save(rows);
};
<SpreadsheetGrid ref={gridRef} rows={rows} onRowsChange={setRows} columns={columns} showValidationMarks={showErrors} />- To show row numbers in messages, use
getRowNumber(rowKey), which returns the same number as the row number column on screen (with label rows it differs from view position + 1). getInvalidCells()is clientSide (rows) only. serverSide (dataSource) doesn't hold all rows, so it returns an empty array with a warning in development. To refuse values with server-side checks, rejectupdateRows(the values are restored and the save failure bar shows).
Message language
The built-in check messages ("Enter a number" and so on) are texts in localeText. The defaults are Japanese; for English, pass localeTextEn from English and text. Messages returned by validate appear as written.
The list of column fields is in "Validation" in GridColumn.
Time, date-time and calendar editors
The time / datetime editors and date's picker calendar (reading typed text, the calendar, a list of times, a time input for date-times)
Change tracking
Mark edited cells as unsaved with trackChanges, take out what to send with getChanges, and clear the marks with markChangesSaved after saving