React Validator - CustomRule
A rule with custom validation logic.
To specify the custom rule, set the type to "custom" and declare the validationCallback function.
You can also set a custom message, specify whether empty values are valid, and whether the rule should be re-evaluated, even if the target value is the same.
See Also
ignoreEmptyValue
If true, the validationCallback is not executed for null, undefined, false, and empty strings.
message
Specifies the message that is shown if the rule is broken.
An error message can be specified as follows:
Hard-code the message
App.js- import React from 'react';
- import 'devextreme/dist/css/dx.light.css';
- import { TextBox } from 'devextreme-react/text-box';
- import {
- Validator,
- CustomRule
- } from 'devextreme-react/validator';
- class App extends React.Component {
- render() {
- return (
- <TextBox>
- <Validator>
- <CustomRule
- message="My custom message" />
- </Validator>
- </TextBox>
- );
- }
- }
- export default App;
Hide the message
App.js- import React from 'react';
- import 'devextreme/dist/css/dx.light.css';
- import { TextBox } from 'devextreme-react/text-box';
- import {
- Validator,
- CustomRule
- } from 'devextreme-react/validator';
- class App extends React.Component {
- render() {
- return (
- <TextBox>
- <Validator>
- <CustomRule
- message="" />
- </Validator>
- </TextBox>
- );
- }
- }
- export default App;
Display the editor's name in the message
App.js- import React from 'react';
- import 'devextreme/dist/css/dx.light.css';
- import { TextBox } from 'devextreme-react/text-box';
- import {
- Validator,
- CustomRule
- } from 'devextreme-react/validator';
- class App extends React.Component {
- render() {
- return (
- <TextBox>
- {/* The error message will be "Password is invalid" */}
- <Validator name="Password">
- <CustomRule />
- </Validator>
- </TextBox>
- );
- }
- }
- export default App;
reevaluate
Indicates whether the rule should be always checked for the target value or only when the target value changes.
If you set this property to false, the rule is checked only when you change an editor's value. If you enter a value and validate the editor's value twice, the validation callback is executed only once.
If you set this property to true, the rule is checked every time an editor is validated. If you enter a value and validate the editor's value twice, the validation callback is executed twice.
validationCallback
A function that validates the target value.
An object that defines validation parameters.
| Name | Type | Description |
|---|---|---|
| column | The column which the cell being validated belongs to. Exists only when you validate a built-in editor in DataGrid or TreeList. | |
| data | Current row data. Exists only when you validate DataGrid or TreeList cell values. | |
| formItem | The form item being validated. Exists only when you validate a built-in editor in the Form UI component. | |
| rule | The rule being checked. | |
| validator | The Validator object that initiated the validation. | |
| value | | | The validated value. |
The column which the cell being validated belongs to. Exists only when you validate a built-in editor in DataGrid or TreeList.
Current row data. Exists only when you validate DataGrid or TreeList cell values.
The form item being validated. Exists only when you validate a built-in editor in the Form UI component.
The rule being checked.
The Validator object that initiated the validation.
The validated value.
true if the value is valid; otherwise, false.
The following code sample shows a validationCallback example. The validateNumber function accepts a number and returns true if the number is even and false if it is odd:
- import React from 'react';
- import 'devextreme/dist/css/dx.light.css';
- import NumberBox from 'devextreme-react/number-box';
- import Validator, {
- CustomRule
- } from 'devextreme-react/validator';
- class App extends React.Component {
- validateNumber(e) {
- return e.value % 2 == 0;
- }
- render() {
- return (
- <NumberBox defaultValue={4}>
- <Validator>
- <CustomRule
- validationCallback={validateNumber}
- message="An even number is expected"
- />
- </Validator>
- </NumberBox>
- );
- }
- }
- export default App;
The code snippet below demonstrates how to implement multiple validation messages in one CustomRule:
- import React from 'react';
- import 'devextreme/dist/css/dx.light.css';
- import NumberBox from 'devextreme-react/number-box';
- import Validator, {
- CustomRule
- } from 'devextreme-react/validator';
- class App extends React.Component {
- function validateNumber(e) {
- switch (true) {
- case (e.value < 0):
- e.rule.message = "Enter a number greater than 0";
- return false;
- case (e.value > 100):
- e.rule.message = "Enter a number less than 100";
- return false;
- case (e.value % 2 !=0):
- e.rule.message = "Enter an even number";
- return false;
- default:
- return true;
- }
- }
- render() {
- return (
- <NumberBox>
- <Validator>
- <CustomRule
- validationCallback={validateNumber}
- />
- </Validator>
- </NumberBox>
- );
- }
- }
- export default App;