Forms Support

The DateInput supports a native KendoReact implementation of HTML5 form validation.

The forms supports enables you to validate input values and prevent the submission of forms which are in an invalid state.

Functionality and Features

Setting Validation Requirements

The DateInput supports props for setting basic validation requirements—for example, the minimum and maximum date. The min, max, and required properties enable the user to pre-define minimum requirements which define the validity state of the component and allow a successful form submission.

import * as React from 'react';
import * as ReactDOM from 'react-dom';

import { DateInput } from '@progress/kendo-react-dateinputs';

class App extends React.Component {
    constructor(props) {
        super(props);

        this.state = {
            success: false
        };
    }
    render() {
        const today = new Date();

        return (
            <div className="row example-wrapper">
                <div className="col-xs-12 col-sm-6 offset-sm-3 example-col">
                    <div className="card">
                        <div className="card-block">
                            <form className="k-form" onSubmit={this.handleSubmit}>
                                <fieldset>
                                    <legend>Please select dates of your stay:</legend>
                                    <label className="k-form-field">
                                        <span>Check-In</span>
                                        <DateInput
                                            width="100%"
                                            name="checkin"
                                            required={true}
                                            min={today}
                                        />
                                    </label>
                                    <label className="k-form-field">
                                        <span>Check-Out</span>
                                        <DateInput
                                            width="100%"
                                            name="checkout"
                                            required={true}
                                            min={today}
                                        />
                                    </label>
                                </fieldset>
                                <input type="submit" className="k-button k-primary" value="Search" />
                            </form>
                        </div>
                    </div>
                </div>
                {this.state.success && (
                    <div
                        className="alert alert-success"
                        style={{ position: 'absolute' }}
                    >
                        Form submitted!
                    </div>)}
            </div>
        );
    }

    handleSubmit = (event) => {
        event.preventDefault();
        this.setState({ success: true });
        setTimeout(() => { this.setState({ success: false }); }, 3000);
    }
}

ReactDOM.render(
    <App />,
    document.querySelector('my-app')
);

Setting Custom Validation Messages

You can implement custom error messages which will be displayed when the DateInput is in an invalid state and the user tries to submit the form. To render a custom validation message, set the validationMessage prop.

import * as React from 'react';
import * as ReactDOM from 'react-dom';

import { DateInput } from '@progress/kendo-react-dateinputs';

class App extends React.Component {
    constructor(props) {
        super(props);

        this.state = {
            success: false
        };
    }
    render() {
        const today = new Date();
        const checkInValidationMessage = "Dates in the past are not valid Check-In dates.";
        const checkOutValidationMessage = "The Check-Out date is required.";

        return (
            <div className="row example-wrapper">
                <div className="col-xs-12 col-sm-6 offset-sm-3 example-col">
                    <div className="card">
                        <div className="card-block">
                            <form className="k-form" onSubmit={this.handleSubmit}>
                                <fieldset>
                                    <legend>Please select dates of your stay:</legend>
                                    <label className="k-form-field">
                                        <span>Check-In</span>
                                        <DateInput
                                            width="100%"
                                            name="checkin"
                                            defaultValue={new Date(2018, 1, 20)}
                                            required={true}
                                            min={today}
                                            validationMessage={checkInValidationMessage}
                                        />
                                    </label>
                                    <label className="k-form-field">
                                        <span>Check-Out</span>
                                        <DateInput
                                            width="100%"
                                            name="checkout"
                                            required={true}
                                            min={today}
                                            validationMessage={checkOutValidationMessage}
                                        />
                                    </label>
                                </fieldset>
                                <input type="submit" className="k-button k-primary" value="Search" />
                            </form>
                        </div>
                    </div>
                </div>
                {this.state.success && (
                    <div
                        className="alert alert-success"
                        style={{ position: 'absolute' }}
                    >
                        Form submitted!
                    </div>)}
            </div>
        );
    }

    handleSubmit = (event) => {
        event.preventDefault();
        this.setState({ success: true });
        setTimeout(() => { this.setState({ success: false }); }, 3000);
    }
}

ReactDOM.render(
    <App />,
    document.querySelector('my-app')
);

Enforcing the Validity State

The DateInput enables you to set its validity state by utilizing its props. Setting the validity state allows for the implementation of complex validation scenarios that are based on multiple sources. To override the current validity state and apply the corresponding styles, use the valid prop.

import * as React from 'react';
import * as ReactDOM from 'react-dom';

import { DateInput } from '@progress/kendo-react-dateinputs';

class App extends React.Component {
    constructor(props) {
        super(props);

        this.state = {
            checkin: null,
            checkout: null,
            additionalComments: '',
            success: false
        };
    }
    render() {
        const today = new Date();

        return (
            <div className="row example-wrapper">
                <div className="col-xs-12 col-sm-6 offset-sm-3 example-col">
                    <div className="card">
                        <div className="card-block">
                            <form className="k-form" onSubmit={this.handleSubmit}>
                                <fieldset>
                                    <legend>Please select dates of your stay:</legend>
                                    <label className="k-form-field">
                                        <span>Check-In</span>
                                        <DateInput
                                            width="100%"
                                            name="checkin"
                                            required={true}
                                            format="dd-MMM-yyyy"
                                            spinners={true}
                                            min={today}

                                            value={this.state.checkin}
                                            onChange={this.handleChange}
                                            validationMessage={this.state.checkin === null
                                                ? 'Check-In date is required!'
                                                : 'Check-In date cannot be in the past!'}
                                        />
                                    </label>
                                    <label className="k-form-field">
                                        <span>Check-Out</span>
                                        <DateInput
                                            width="100%"
                                            name="checkout"
                                            required={true}
                                            format="dd-MMM-yyyy"
                                            spinners={true}

                                            valid={(this.state.checkin !== null && this.state.checkout !== null)
                                                && (this.state.checkin.getTime() < this.state.checkout.getTime())}

                                            value={this.state.checkout}
                                            onChange={this.handleChange}
                                            validationMessage={this.state.checkout === null
                                                ? 'Check-Out date is required!'
                                                : 'Check-Out date cannot be before Check-In date!'}
                                        />
                                    </label>
                                    <label className="k-form-field">
                                        <span>Additional Comments</span>
                                        <textarea
                                            className="k-textarea"
                                            style={{ width: '100%' }}
                                            name="additionalComments"
                                            value={this.state.additionalComments}
                                            onChange={this.handleChange}
                                        />
                                    </label>
                                </fieldset>
                                <input type="submit" className="k-button k-primary" value="Search" />
                            </form>
                        </div>
                    </div>
                </div>
                {this.state.success && (
                    <div
                        className="alert alert-success"
                        style={{ position: 'absolute' }}
                    >
                        Form submitted!
                    </div>)}
            </div>
        );
    }

    handleChange = (event) => {
        this.setState({
            [event.target.name]: event.target.value
        });
    }

    handleSubmit = (event) => {
        event.preventDefault();
        this.setState({ success: true });
        setTimeout(() => { this.setState({ success: false }); }, 3000);
    }
}

ReactDOM.render(
    <App />,
    document.querySelector('my-app')
);

Disabling the Validation Styling

By default, to achieve a visual representation of its invalid state, the DateInput applies the k-state-invalid class name to the wrapping component. To disable the utilization of k-state-invalid, set the validityStyles property to false.

import * as React from 'react';
import * as ReactDOM from 'react-dom';

import { DateInput } from '@progress/kendo-react-dateinputs';

class App extends React.Component {
    constructor(props) {
        super(props);

        this.state = {
            checkin: new Date(2022, 1, 20),
            checkout: null,
            additionalComments: '',
            success: false
        };
    }
    render() {
        const today = new Date();

        return (
            <div className="row example-wrapper">
                <div className="col-xs-12 col-sm-6 offset-sm-3 example-col">
                    <div className="card">
                        <div className="card-block">
                            <form className="k-form" onSubmit={this.handleSubmit}>
                                <fieldset>
                                    <legend>Please select dates of your stay:</legend>
                                    <label className="k-form-field">
                                        <span>Check-In</span>
                                        <DateInput
                                            width="100%"
                                            name="checkin"
                                            required={true}
                                            format="dd-MMM-yyyy"
                                            spinners={true}
                                            min={today}
                                            value={this.state.checkin}
                                            onChange={this.handleChange}
                                            validationMessage={this.state.checkin === null
                                                ? 'Check-In date is required!'
                                                : 'Check-In date cannot be in the past!'}
                                        />
                                    </label>
                                    <label className="k-form-field">
                                        <span>Check-Out</span>
                                        <DateInput
                                            width="100%"
                                            validityStyles={false}

                                            name="checkout"
                                            required={true}
                                            format="dd-MMM-yyyy"
                                            spinners={true}
                                            valid={(this.state.checkin !== null && this.state.checkout !== null)
                                                && (this.state.checkin.getTime() < this.state.checkout.getTime())}
                                            value={this.state.checkout}
                                            onChange={this.handleChange}
                                            validationMessage={this.state.checkout === null
                                                ? 'Check-Out date is required!'
                                                : 'Check-Out date cannot be before Check-In date!'}
                                        />
                                    </label>
                                    <label className="k-form-field">
                                        <span>Additional Comments</span>
                                        <textarea
                                            className="k-textarea"
                                            style={{ width: '100%' }}
                                            name="additionalComments"
                                            value={this.state.additionalComments}
                                            onChange={this.handleChange}
                                        />
                                    </label>
                                </fieldset>
                                <input type="submit" className="k-button k-primary" value="Search" />
                            </form>
                        </div>
                    </div>
                </div>
                {this.state.success && (
                    <div
                        className="alert alert-success"
                        style={{ position: 'absolute' }}
                    >
                        Form submitted!
                    </div>)}
            </div>
        );
    }

    handleChange = (event) => {
        this.setState({
            [event.target.name]: event.target.value
        });
    }

    handleSubmit = (event) => {
        event.preventDefault();
        this.setState({ success: true });
        setTimeout(() => { this.setState({ success: false }); }, 3000);
    }
}

ReactDOM.render(
    <App />,
    document.querySelector('my-app')
);

In this article