Forms Support

The ComboBox 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 ComboBox supports props for setting basic validation requirements. The required property enables the user to pre-define a selection that is required for a successful form submission.

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

import { ComboBox } from '@progress/kendo-react-dropdowns';
import { countries } from './countries';

class App extends React.Component {
    state = {
        success: false
    };

    render() {
        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>Select your flight route:</legend>
                                    <div className="mb-3">
                                        <ComboBox
                                            style={{ width: '100%' }}
                                            label="Departure Country"
                                            name="departureCountry"
                                            data={countries}
                                            required={true}
                                        />
                                    </div>
                                    <div>
                                        <ComboBox
                                            style={{ width: '100%' }}
                                            label="Arrival Country"
                                            name="arrivalCountry"
                                            data={countries}
                                            required={true}
                                        />
                                    </div>
                                </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 ComboBox 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 { ComboBox } from '@progress/kendo-react-dropdowns';
import { countries } from './countries';

class App extends React.Component {
    state = {
        success: false
    };

    render() {
        const departureValidationMessage = 'Please select a departure country!';
        const arrivalValidationMessage = 'Please select an arrival country!';

        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>Select your flight route:</legend>
                                    <div className="mb-3">
                                        <ComboBox
                                            style={{ width: '100%' }}
                                            label="Departure Country"
                                            name="departureCountry"
                                            data={countries}
                                            required={true}
                                            validationMessage={departureValidationMessage}
                                        />
                                    </div>
                                    <div>
                                        <ComboBox
                                            style={{ width: '100%' }}
                                            label="Arrival Country"
                                            name="arrivalCountry"
                                            data={countries}
                                            required={true}
                                            validationMessage={arrivalValidationMessage}
                                        />
                                    </div>
                                </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 ComboBox enables you to set its validity state by utilizing its exposed 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 { ComboBox } from '@progress/kendo-react-dropdowns';
import { countries } from './countries';

class App extends React.Component {
    state = {
        departureCountry: null,
        arrivalCountry: null,
        additionalComments: '',
        success: false
    };

    render() {
        console.log(this.state.departureCountry, this.state.arrivalCountry);
        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>Select your flight route:</legend>
                                    <div className="mb-3">
                                        <ComboBox
                                            style={{ width: '100%' }}
                                            value={this.state.departureCountry}
                                            onChange={this.handleChange}
                                            label="Departure Country"
                                            name="departureCountry"
                                            data={countries}
                                        />
                                    </div>
                                    <div className="mb-3">
                                        <ComboBox
                                            style={{ width: '100%' }}
                                            value={this.state.arrivalCountry}
                                            onChange={this.handleChange}
                                            label="Arrival Country"
                                            name="arrivalCountry"
                                            data={countries}
                                            valid={(this.state.departureCountry !== null && this.state.arrivalCountry !== null)}
                                            validationMessage={this.state.departureCountry === null
                                                ? 'Please select a Departure Country first!'
                                                : 'Please select an Arrival Country'}
                                        />
                                    </div>
                                    <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 ComboBox 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 { ComboBox } from '@progress/kendo-react-dropdowns';
import { countries } from './countries';

class App extends React.Component {
    state = {
        departureCountry: null,
        arrivalCountry: null,
        additionalComments: '',
        success: false
    };

    render() {
        const departureValidationMessage = 'Please select a departure country!';
        const arrivalValidationMessage = 'Please select an arrival country!';

        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>Select your flight route:</legend>
                                    <div className="mb-3">
                                        <ComboBox
                                            style={{ width: '100%' }}
                                            value={this.state.departureCountry}
                                            onChange={this.handleChange}
                                            label="Departure Country"
                                            name="departureCountry"
                                            data={countries}
                                            required={true}
                                            validationMessage={departureValidationMessage}
                                        />
                                    </div>
                                    <div className="mb-3">
                                        <ComboBox
                                            validityStyles={false}
                                            style={{ width: '100%' }}
                                            value={this.state.arrivalCountry}
                                            onChange={this.handleChange}
                                            label="Arrival Country"
                                            name="arrivalCountry"
                                            data={countries}
                                            required={true}
                                            validationMessage={arrivalValidationMessage}
                                        />
                                    </div>
                                    <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')
);
 /