Data Validation
The Kendo UI for Vue Spreadsheet lets you control the data that users enter in the cells. A validation rule checks every value that is typed in a cell and either rejects it or warns the user.
The following example demonstrates the validation rules in action. The Quantity column accepts only numbers between 1 and 100, the Status column provides a list of allowed values, and the Discount % column warns the user about unusual values.
Using the Data Validation Dialog
By default, the Data tab of the toolbar contains the Data Validation tool. To set a rule, users select a range of cells, open the dialog, and configure the following options:
| Option | Description |
|---|---|
| Cell Range | The range of cells to which the rule applies, for example, B2:B9. |
| Criteria | The type of data that the cells accept—Any value, Number, Text, Date, Custom formula, or List. |
| Comparer | The comparison that is applied to the value—between, not between, equal to, not equal to, greater than, less than, greater than or equal to, or less than or equal to. |
| Min, Max, or Value | The values the entered data is compared to. |
| Ignore blank | Specifies whether the cell can be left empty. |
| On invalid data | Specifies whether the Spreadsheet rejects the invalid value (Reject input) or displays a warning and lets the user keep it (Show warning). |
| Show hint | Displays a tooltip with a title and a message when the user selects the cell. |
To remove the rule, set the Criteria to Any value.
Setting the Validation in the Data
To define the validation rules when the data is loaded, set the validation field of the cell in the defaultProps sheets. The following example restricts a cell to numbers between 1 and 100 and rejects every other value:
const sheets = [
{
name: 'Orders',
rows: [
{
cells: [
{
value: 20,
validation: {
dataType: 'number',
comparerType: 'between',
from: '1',
to: '100',
allowNulls: false,
type: 'reject',
tooltipTitleTemplate: 'Quantity',
tooltipMessageTemplate: 'Enter a number between {0} and {1}.',
titleTemplate: 'Invalid quantity',
messageTemplate: 'The quantity must be between {0} and {1}.'
}
}
]
}
]
}
];
The validation object supports the following fields:
| Field | Description |
|---|---|
dataType | The type of the accepted data—number, text, date, custom, or list. |
comparerType | The comparison that is applied—between, notBetween, equalTo, notEqualTo, greaterThan, lessThan, greaterThanOrEqualTo, or lessThanOrEqualTo. For the list data type, use equalTo. For the custom data type, the rule is the formula in from. |
from | The value or formula to compare to. For a between or notBetween comparison, this is the lower bound. For a list, the allowed items are quoted and separated by commas, for example, '"New","Shipped"'. |
to | The upper bound for a between or notBetween comparison. |
allowNulls | Specifies whether the cell can be empty. |
type | The behavior on invalid data—reject or warning. The default value is warning. |
showButton | Specifies whether the cells with a list or a date rule display a drop-down button. |
tooltipTitleTemplate and tooltipMessageTemplate | The title and the message of the hint that is displayed when the cell is selected. |
titleTemplate and messageTemplate | The title and the message of the dialog that is displayed when the entered value is invalid. |
The templates can use the {0} and {1} placeholders, which are replaced with the evaluated from and to values.
Setting the Validation Programmatically
To apply or remove a rule at runtime, use the executeCommand method of the Spreadsheet with the EditValidationCommand command. Pass the range in the operatingRange option and the rule in the value option. To remove the validation, pass null as a value.
spreadsheet.value.executeCommand({
command: 'EditValidationCommand',
options: {
operatingRange: spreadsheet.value.activeSheet().range('E2:E9'),
value: {
dataType: 'number',
comparerType: 'between',
from: '0',
to: '30',
type: 'reject'
}
}
});
The Reject discounts above 30 in E2:E9 button in the demo above uses this approach.