Validation in React In-place Editor
03 Sep 202623 minutes to read
React In-place Editor component supports validation and it can be achieved by adding rules to the validationRules property, its child property key must be same as name property, otherwise validation not performed. Submitting data to the server or calling the validate method validation executed.
Validation Rules
The React In-place Editor supports the following validation rules:
| Rules | Description | Example |
|---|---|---|
required |
The input element must have any input values | a or 1 or - |
email |
The input element must have valid email format values |
[email protected] |
url |
The input element must have valid url format values |
http://syncfusion.com/ |
date |
The input element must have valid date format values |
12/25/2019 |
dateIso |
The input element must have valid dateIso format values |
2019-12-25 |
number |
The input element must have valid number format values |
1.0 or 1 |
maxLength |
Input value must have less than or equal to maxLength character length |
if maxLength: 5, [check] is valid and [checking] is invalid |
minLength |
Input value must have at least minLength characters |
if minLength: 5, [testing] is valid and [test] is invalid |
rangeLength |
Input value must be between the specified rangeLength character length range |
if rangeLength: [4,5], [test] is valid and [key] is invalid |
range |
Input value must be within the specified range
|
if range: [4,5], [4] is valid and [6] is invalid |
max |
Input value must have less than or equal to max number |
if max: 3, [3] is valid and [4] is invalid |
min |
Input value must have less than or equal to min number |
if min: 4, [5] is valid and [2] is invalid |
regex |
Input value must have valid regex format |
if regex: '^[A-z]+$', [a] is valid and [1] is invalid |
Step by step validation configuration
Configure validation in the React In-place Editor by following these steps:
Step 1: The name property is mandatory for validation to work. The specified name must match the corresponding key in the validationRules property.
Step 2: Bind the name property value to the validationRules property. For example, if the name property is Number, create a corresponding Number key in validationRules and assign validation rules such as maxLength. You can apply any of the built-in validation rules listed in the table above.
[Class-component]
import { InPlaceEditorComponent } from "@syncfusion/ej2-react-inplace-editor";
import * as React from 'react';
class App extends React.Component {
constructor() {
super(...arguments);
this.validationRules = { Number: { maxLength: 5 } };
}
render() {
return (<div id="container">
<table className="table-section">
<tbody>
<tr>
<td className="col-lg-6 col-md-6 col-sm-6 col-xs-6">
<InPlaceEditorComponent mode="Inline" type="Numeric" name="Number" validationRules={this.validationRules}/>
</td>
</tr>
</tbody>
</table>
</div>);
}
}
export default App;import { InPlaceEditorComponent } from "@syncfusion/ej2-react-inplace-editor";
import * as React from 'react';
class App extends React.Component {
constructor() {
super(...arguments);
this.validationRules = { Number: { maxLength: 5 } };
}
public render() {
return (
<div id="container">
<table className="table-section">
<tbody>
<tr>
<td className="col-lg-6 col-md-6 col-sm-6 col-xs-6">
<InPlaceEditorComponent
mode="Inline"
type="Numeric"
name="Number"
validationRules={this.validationRules}
/>
</td>
</tr>
</tbody>
</table>
</div>
);
}
}
export default App;[Functional-component]
import { InPlaceEditorComponent } from "@syncfusion/ej2-react-inplace-editor";
import * as React from 'react';
function App() {
let validationRules = { Number: { maxLength: 5 } };
return (<div id="container">
<table className="table-section">
<tbody>
<tr>
<td className="col-lg-6 col-md-6 col-sm-6 col-xs-6">
<InPlaceEditorComponent mode="Inline" type="Numeric" name="Number" validationRules={validationRules}/>
</td>
</tr>
</tbody>
</table>
</div>);
}
export default App;import { InPlaceEditorComponent } from "@syncfusion/ej2-react-inplace-editor";
import * as React from 'react';
function App () {
let validationRules = { Number: { maxLength: 5 } };
return (
<div id="container">
<table className="table-section">
<tbody>
<tr>
<td className="col-lg-6 col-md-6 col-sm-6 col-xs-6">
<InPlaceEditorComponent
mode="Inline"
type="Numeric"
name="Number"
validationRules={validationRules}
/>
</td>
</tr>
</tbody>
</table>
</div>
);
}
export default App;In the following sample, the first editor displays a validation error when submitted without a date selection. The second editor is configured with the validating event. In the event handler, the errorMessage is customized and displayed below the DatePicker element.
[Class-component]
import { InPlaceEditorComponent } from '@syncfusion/ej2-react-inplace-editor';
import * as React from 'react';
class App extends React.Component {
model = { placeholder: 'Select date' };
textboxvalidation = { Today: { required: true } };
datevalidation = { TodayCustom: { required: true } };
validating(e) {
e.errorMessage = 'Field should not be empty';
}
render() {
return (<div id='container'>
<table className="table-section">
<tbody>
<tr>
<td className="col-lg-6 col-md-6 col-sm-6 col-xs-6 control-title"> Default Error Message </td>
<td className="col-lg-6 col-md-6 col-sm-6 col-xs-6">
<InPlaceEditorComponent id='textBox' mode='Inline' type='Date' name='Today' emptyText='dd/mm/yyyy' model={this.model} validationRules={this.textboxvalidation}/>
</td>
</tr>
<tr>
<td className="col-lg-6 col-md-6 col-sm-6 col-xs-6 control-title"> Customized Error Message </td>
<td className="col-lg-6 col-md-6 col-sm-6 col-xs-6">
<InPlaceEditorComponent id='date' mode='Inline' type='Date' name='TodayCustom' emptyText='dd/mm/yyyy' model={this.model} validationRules={this.datevalidation} validating={this.validating = this.validating.bind(this)}/>
</td>
</tr>
</tbody>
</table>
</div>);
}
}
export default App;import { InPlaceEditorComponent, ValidateEventArgs } from '@syncfusion/ej2-react-inplace-editor';
import * as React from 'react';
class App extends React.Component<{},{}> {
public model = { placeholder: 'Select date' };
public textboxvalidation = { Today: { required: true } };
public datevalidation = { TodayCustom: { required: true } };
public validating(e: ValidateEventArgs): void {
e.errorMessage = 'Field should not be empty';
}
public render() {
return (
<div id='container'>
<table className="table-section">
<tbody>
<tr>
<td className="col-lg-6 col-md-6 col-sm-6 col-xs-6 control-title"> Default Error Message </td>
<td className="col-lg-6 col-md-6 col-sm-6 col-xs-6">
<InPlaceEditorComponent id='textBox' mode='Inline' type='Date' name='Today' emptyText='dd/mm/yyyy' model={this.model} validationRules={this.textboxvalidation} />
</td>
</tr>
<tr>
<td className="col-lg-6 col-md-6 col-sm-6 col-xs-6 control-title"> Customized Error Message </td>
<td className="col-lg-6 col-md-6 col-sm-6 col-xs-6">
<InPlaceEditorComponent id='date' mode='Inline' type='Date' name='TodayCustom' emptyText='dd/mm/yyyy' model={this.model} validationRules={this.datevalidation} validating={ this.validating = this.validating.bind(this) } />
</td>
</tr>
</tbody>
</table>
</div>
);
}
}
export default App;[Functional-component]
import { InPlaceEditorComponent } from '@syncfusion/ej2-react-inplace-editor';
import * as React from 'react';
function App() {
let model = { placeholder: 'Select date' };
let textboxvalidation = { Today: { required: true } };
let datevalidation = { TodayCustom: { required: true } };
function validating(e) {
e.errorMessage = 'Field should not be empty';
}
return (<div id='container'>
<table className="table-section">
<tbody>
<tr>
<td className="col-lg-6 col-md-6 col-sm-6 col-xs-6 control-title"> Default Error Message </td>
<td className="col-lg-6 col-md-6 col-sm-6 col-xs-6">
<InPlaceEditorComponent id='textBox' mode='Inline' type='Date' name='Today' emptyText='dd/mm/yyyy' model={model} validationRules={textboxvalidation}/>
</td>
</tr>
<tr>
<td className="col-lg-6 col-md-6 col-sm-6 col-xs-6 control-title"> Customized Error Message </td>
<td className="col-lg-6 col-md-6 col-sm-6 col-xs-6">
<InPlaceEditorComponent id='date' mode='Inline' type='Date' name='TodayCustom' emptyText='dd/mm/yyyy' model={model} validationRules={datevalidation} validating={validating = validating.bind(this)}/>
</td>
</tr>
</tbody>
</table>
</div>);
}
export default App;import { InPlaceEditorComponent, ValidateEventArgs } from '@syncfusion/ej2-react-inplace-editor';
import * as React from 'react';
function App () {
let model : object= { placeholder: 'Select date' };
let textboxvalidation = { Today: { required: true } };
let datevalidation = { TodayCustom: { required: true } };
function validating(e: ValidateEventArgs): void {
e.errorMessage = 'Field should not be empty';
}
return (
<div id='container'>
<table className="table-section">
<tbody>
<tr>
<td className="col-lg-6 col-md-6 col-sm-6 col-xs-6 control-title"> Default Error Message </td>
<td className="col-lg-6 col-md-6 col-sm-6 col-xs-6">
<InPlaceEditorComponent id='textBox' mode='Inline' type='Date' name='Today' emptyText='dd/mm/yyyy' model={model} validationRules={textboxvalidation} />
</td>
</tr>
<tr>
<td className="col-lg-6 col-md-6 col-sm-6 col-xs-6 control-title"> Customized Error Message </td>
<td className="col-lg-6 col-md-6 col-sm-6 col-xs-6">
<InPlaceEditorComponent id='date' mode='Inline' type='Date' name='TodayCustom' emptyText='dd/mm/yyyy' model={model} validationRules={datevalidation} validating={ validating = validating.bind(this) } />
</td>
</tr>
</tbody>
</table>
</div>
);
}
export default App;-
For more details about validation configuration, refer this documentation section.
-
For custom validation except specifying validationRules, specify errorMessage at validating event, message will be shown when the value is
Empty.