Validation

Overview

rs-grid supports per-column validators. A validator is called before a cell edit is committed. If it returns an error, the edit is cancelled and a callback is fired — the data layer is never touched.

StartEdit → user types → CommitEdit
  → validator(&value)
      Ok(())  → patch written → on_change()
      Err(msg) → CancelEdit   → on_validation_error(row, col, msg)

Adding a validator

Set ColumnDef::validator with a CellValidator:

use rs_grid_core::column::{CellValidator, ColumnDef};

let mut col = ColumnDef::new("salary", "Salary", 120.0);
col.validator = Some(CellValidator::new(|v| {
    v.parse::<f64>()
        .ok()
        .filter(|&n| n >= 0.0)
        .map(|_| ())
        .ok_or_else(|| "Must be a positive number".to_string())
}));

The closure receives the raw cell string and must return:

  • Ok(()) — accept the value
  • Err(String) — reject with an error message

Listening to validation errors

Leptos
Vanilla JS
Dioxus
Yew

Pass on_validation_error as a prop to <GridCanvas>:

let set_error = /* RwSignal<String> */;

view! {
    <GridCanvas
        model=model
        on_validation_error=Some(Box::new(move |_row, col, msg| {
            set_error.set(format!("[{col}] {msg}"));
        }))
    />
}

The callback fires synchronously after the edit is cancelled. Arguments:

ArgumentTypeDescription
rowu64Logical row index of the rejected cell
col_key&strKey of the column whose validator fired
message&strError string returned by the validator

Behaviour

  • Validation runs only on user edits (CommitEdit). Programmatic writes via GridModel::set_cell bypass the validator.
  • On rejection, CancelEdit is applied automatically — the cell reverts to its previous value. No undo entry is created.
  • Select editors are also validated; the selected option value is passed to the validator before commit.
  • Validators are synchronous. For async validation (e.g. server checks), commit optimistically and revert via GridCommand::Undo if the check fails.

See also