Handsontable Validator Development
Signature
function myValidator(value, callback) {
// `this` is bound to the cell's cellProperties object
if (this.allowEmpty && (value === null || value === undefined || value === '')) {
return callback(true);
}
callback(isValid(value)); // true = valid, false = invalid
}
Rules
- Call the callback on every path; a missed call hangs the validation pipeline and silently blocks editing.
thisiscellProperties: usethis.allowEmpty,this.instance, other cell meta. Use afunctionbody, not an arrow function (arrows break thethisbinding).- Validators only determine validity; data, DOM, and cell state stay untouched.
- The callback may run after async work; the editor stays in WAITING state until it resolves.
Files
src/validators/{validatorName}/
{validatorName}.ts # Validator function
index.ts # Re-exports
Registry: src/validators/registry.ts.
import { registerValidator } from '../../validators/registry';
registerValidator('myValidator', myValidator);
A new built-in validator also needs its entry in src/validators/features.json (validator:<name_in_snake_case>, licensing: "none"; copy a neighbor), and a cell type that uses it lists it in its requires.allOf. The unit suite fails until both exist. A validator folder that is never registered under a name (maxLengthValidator, regExpValidator) goes in the skip list of scripts/utils/feature-manifest.js instead, with the reason. Run npm run generate:feature-manifest --prefix handsontable and commit what it writes. Rules: src/featureManifest/AGENTS.md.
Triggers
- The editor system during
finishEditing(). validateCells(),validateRows(),validateColumns().- Invalid cells get the
htInvalidclass from the renderer pipeline, not the validator.
Reference implementations
src/validators/numericValidator/numericValidator.ts-allowEmptysupport.src/validators/dateValidator/dateValidator.tssrc/validators/autocompleteValidator/autocompleteValidator.ts- list of allowed values.
Correcting cell values inside a validator
When a validator writes a corrected value with setDataAtCell, pass a source string ending in 'Validator':
this.instance.setDataAtCell(row, col, correctedValue, 'myCustomValidator');
validateChanges() in core.ts uses the suffix to fold the correction into the change that triggered it, so that change still applies with the corrected value and fires afterChange. Without it, the correction is a separate write that wins over the triggering change (the last write to a cell wins), and the triggering change is dropped with no afterChange.
Common mistakes
- Reading a meta flag that an editor forces in
prepare().DropdownEditor.prepare()writesstrict = trueonto the one cell it prepares, only while selected. A validator readingthis.strictsees the column's ownstrict: falseon every cell never clicked (paste,setDataAtCell(),validateCells(), anyupdateSettings()that rebuilds cell meta), which made astrict: falsedropdown strict only in clicked cells (DEV-2911). For fixed cell-type behavior, pass it to the shared logic explicitly:dropdownValidatorcallsvalidateAgainstSource(this, value, callback, true). Test by writing to a cell that was never selected; selecting it first hides the bug.