JavaScript/jQuery Form - Getting Started

NOTE
Before you start the tutorial, ensure DevExtreme is installed in your application.

The Form component creates a data entry UI for the underlying data object. The interface consists of automatically-arranged label-editor pairs that correspond to data fields. You can change data values, validate user input, and send the resulting data to the server.

This tutorial shows basic Form component configuration.

Each section in this tutorial describes a single configuration step. The complete source code is available in the following GitHub repository:

View on GitHub

Create a Form

Populate the Form Fields

The Form creates a data entry UI when you assign an object to the formData property. The Form creates a simple item (a label-editor pair) for each field in the formData object.

The Form chooses default editors based on value types: a TextBox for string values, a NumberBox for numbers, and a DateBox for dates. You can use the editorType property to specify an editor explicitly. In the following example, the Form uses default editors:

When you implement this code, the Form creates the following simple items: a TextBox for name, a NumberBox for officeNumber, and a DateBox for the hireDate data field.

Configure Simple Items

Use the items[] array to configure all form items. This array can contain strings (formData field names) and objects (item configurations). Use a string to create a label-editor pair (a simple item) with default configuration. To change the default settings, declare an item configuration object: specify the dataField and other properties. The following example configures the HireDate item:

Organize the Form Items

In Columns

The Form can organize items into a fixed number of columns or automatically adjust the layout based on the screen width. Initialize the colCount property as done in the following code to keep a fixed number of columns. To create an adaptive layout instead, configure the screenByWidth and colCountByScreen properties.

An item can span multiple columns. The following example sets the colSpan property for the Notes item to 2 so that it spans two columns.

In Groups

You can use groups to organize data entry forms. Create a group item and nest other items within to add a group. You can nest items of any type in a group, including other group items. You can configure each group's layout separately.

The following code snippet creates two groups. Each group occupies a separate column. The resulting layout looks as follows:

DevExtreme Form: Grouped Items

In Tabs

The Form uses the TabPanel component to display tabbed items. You can specify tab panel settings in the tabPanelOptions object. A tab can contain any item type.

The following example nests a tabbed item in the Personal Information group and configures the tab panel's height property. The resulting Form looks as follows:

DevExtreme Form: Tabbed Items

Add an Empty Space

Use an empty item to add an empty space between neighboring items. Assign "empty" to the itemType property to create an empty item, and specify the colSpan property to define how many columns the empty item should span. For a list of available properties, refer to the Empty Item section.

In the following example, the empty item spans two columns. The resulting layout looks as follows:

DevExtreme Form: Empty Item

Configure Item Labels

You can configure individual labels in the label object. This object's section lists all available properties.

The following properties apply to all labels in the Form:

The following code configures the labelLocation property to place labels on top of editors. The example also configures the Notes item's label.alignment property to position label text in the center:

DevExtreme Form: Item Labels

Modify the Form at Runtime

You can change any properties of the form, its items, or editors at runtime. Use the option(optionName, optionValue) method to update a Form property, and the itemOption(id, option, value) method to update an item property's value. The following code modifies the readOnly property's value:

Validate the Form Data

DevExtreme includes a validation engine that checks edited values before they are saved. This engine supports different validation rule types.

Specify validation rules for a simple item in the validationRules[] array. You can specify an item's isRequired property to implicitly apply the RequiredRule.

The following example sets the isRequired property for the Name item. It also adds the NumericRule for officeNumber and the EmailRule for Email.

You can also call the Form's validate() method to validate all editors simultaneously.

Submit the Form

Add a Button Item and set its useSubmitBehavior property to true to submit a form. You can submit the Form to a server only if input validation is successful.

The useSubmitBehavior property requires that you wrap the dxForm in the HTML form element. You should also set the preventDefault property to true to override the HTML form submit event.

The following code snippet adds a submit button but does not implement the backend. The example displays a confirmation message after a timeout:

Alternatively, if you want to implement custom validation logic, handle the Button click event:

For more information on the Form UI component, refer to the following resources: