Angular 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

Add DevExtreme to your Angular application and use the following code to create a Form:

app.component.html
app.component.ts
  • <dx-form id="form">
  • <!-- Configuration goes here -->
  • </dx-form>
  • import { DxFormModule } from 'devextreme-angular';
  •  
  • @Component({
  • imports: [DxFormModule, /* ... */],
  • // ...
  • })
  • export class AppComponent {
  •  
  • }

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:

app.component.html
app.component.ts
  • <dx-form
  • [formData]="employee">
  • </dx-form>
  • // ...
  • export class AppComponent {
  • employee = {
  • name: 'John Heart',
  • officeNumber: 901,
  • hireDate: new Date(2012, 4, 13)
  • }
  • }

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:

app.component.html
app.component.ts
  • <dx-form
  • [formData]="employee">
  • <dxi-form-item dataField="name"></dxi-form-item>
  • <dxi-form-item dataField="officeNumber"></dxi-form-item>
  • <dxi-form-item
  • dataField="hireDate"
  • [editorOptions]="hireDateOptions">
  • </dxi-form-item>
  • </dx-form>
  • // ...
  • export class AppComponent {
  • employee = {
  • name: 'John Heart',
  • officeNumber: 901,
  • hireDate: new Date(2012, 4, 13)
  • }
  •  
  • hireDateOptions = {
  • disabled: true
  • }
  • }

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.

app.component.html
app.component.ts
  • <dx-form
  • [formData]="employee"
  • [colCount]="2">
  • <dxi-form-item dataField="name"></dxi-form-item>
  • <dxi-form-item dataField="position"></dxi-form-item>
  • <dxi-form-item dataField="hireDate"></dxi-form-item>
  • <dxi-form-item dataField="officeNumber"></dxi-form-item>
  • <dxi-form-item
  • dataField="notes"
  • [colSpan]="2">
  • </dxi-form-item>
  • </dx-form>
  • // ...
  • export class AppComponent {
  • employee = {
  • name: 'John Heart',
  • position: 'CEO',
  • hireDate: new Date(2012, 4, 13),
  • officeNumber: 901,
  • notes: 'John has been in the Audio/Video industry since 1990.'
  • }
  • }

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

app.component.html
app.component.ts
  • <dx-form
  • [formData]="employee"
  • [colCount]="2">
  • <dxi-form-item
  • itemType="group"
  • caption="Personal Information">
  • <dxi-form-item dataField="name"></dxi-form-item>
  • <dxi-form-item dataField="position"></dxi-form-item>
  • <dxi-form-item dataField="hireDate"></dxi-form-item>
  • <dxi-form-item dataField="officeNumber"></dxi-form-item>
  • </dxi-form-item>
  • <dxi-form-item itemType="group" caption="Contacts">
  • <dxi-form-item dataField="phone"></dxi-form-item>
  • <dxi-form-item dataField="skype"></dxi-form-item>
  • <dxi-form-item dataField="email"></dxi-form-item>
  • </dx-item>
  • </dx-form>
  • // ...
  • export class AppComponent {
  • employee = {
  • name: 'John Heart',
  • position: 'CEO',
  • hireDate: new Date(2012, 4, 13),
  • officeNumber: 901,
  • phone: '+1(213) 555-9392',
  • skype: 'jheart_DX_skype',
  • email: 'jheart@dx-email.com'
  • }
  • }

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

app.component.html
app.component.ts
  • <dx-form
  • [formData]="employee"
  • [colCount]="2">
  • <dxi-form-item
  • itemType="group"
  • caption="Employee">
  • <dxi-form-item dataField="name"></dxi-form-item>
  • <dxi-form-item dataField="position"></dxi-form-item>
  • <dxi-form-item dataField="hireDate"></dxi-form-item>
  • <dxi-form-item dataField="officeNumber"></dxi-form-item>
  • </dxi-form-item>
  • <dxi-form-item itemType="group" caption="Personal Information">
  • <dxi-form-item itemType="tabbed">
  • <dxo-form-tab-panel-options [height]="280">
  • </dxo-form-tab-panel-options>
  • <dxi-form-tab title="Contacts">
  • <dxi-form-item dataField="skype"></dxi-form-item>
  • <dxi-form-item dataField="phone"></dxi-form-item>
  • <dxi-form-item dataField="email"></dxi-form-item>
  • </dxi-form-tab>
  • <dxi-form-tab title="Note">
  • <dxi-form-item dataField="notes"></dxi-form-item>
  • </dxi-form-tab>
  • </dxi-form-item>
  • </dxi-form-item>
  • </dx-form>
  • // ...
  • export class AppComponent {
  • employee = {
  • name: 'John Heart',
  • position: 'CEO',
  • hireDate: new Date(2012, 4, 13),
  • officeNumber: 901,
  • phone: '+1(213) 555-9392',
  • skype: 'jheart_DX_skype',
  • email: 'jheart@dx-email.com',
  • notes: 'John has been in the Audio/Video industry since 1990.'
  • }
  • }

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

app.component.html
app.component.ts
  • <dx-form
  • [formData]="employee"
  • [colCount]="2">
  • <dxi-form-item dataField="name"></dxi-form-item>
  • <dxi-form-item dataField="position"></dxi-form-item>
  • <dxi-form-item dataField="hireDate"></dxi-form-item>
  • <dxi-form-item dataField="officeNumber"></dxi-form-item>
  • <dxi-form-item
  • itemType="empty"
  • [colSpan]="2">
  • </dxi-form-item>
  • <dxi-form-item dataField="skype"></dxi-form-item>
  • <dxi-form-item dataField="phone"></dxi-form-item>
  • <dxi-form-item dataField="email"></dxi-form-item>
  • </dx-form>
  • // ...
  • export class AppComponent {
  • employee = {
  • // ...
  • }
  • }

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

app.component.html
  • <dx-form
  • [formData]="employee"
  • [colCount]="2"
  • labelLocation="top">
  • <dxi-form-item dataField="name"></dxi-form-item>
  • <dxi-form-item dataField="position"></dxi-form-item>
  • <dxi-form-item dataField="hireDate"></dxi-form-item>
  • <dxi-form-item dataField="officeNumber"></dxi-form-item>
  • <dxi-form-item
  • dataField="notes"
  • [colSpan]="2">
  • <dxo-form-label alignment="center">
  • </dxo-form-label>
  • </dxi-form-item>
  • </dx-form>

Modify the Form at Runtime

You can change any properties of the form, its items, or editors at runtime. To update a property value, bind it to a component property. The following code modifies the readOnly property's value:

app.component.html
app.component.ts
  • <dx-form
  • [formData]="employee"
  • [readOnly]="isFormReadOnly"
  • >
  • <!-- ... -->
  • </dx-form>
  •  
  • <dx-check-box
  • text="Enable read-only mode"
  • [(value)]="isFormReadOnly">
  • </dx-check-box>
  • import { DxFormModule, DxCheckBoxModule } from 'devextreme-angular';
  •  
  • // ...
  • export class AppComponent {
  • employee = {
  • // ...
  • };
  •  
  • isFormReadOnly = false;
  • }

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.

app.component.html
  • <dx-form
  • [formData]="employee"
  • [colCount]="2">
  • <dxi-form-item dataField="name" [isRequired]="true">
  • </dxi-form-item>
  • <dxi-form-item dataField="officeNumber">
  • <dxi-form-validation-rule type="numeric">
  • </dxi-form-validation-rule>
  • </dxi-form-item>
  • <dxi-form-item dataField="email">
  • <dxi-form-validation-rule type="email">
  • </dxi-form-validation-rule>
  • </dxi-form-item>
  • </dx-form>

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:

app.component.html
app.component.ts
  • <form action="/employee-page" (submit)="handleSubmit($event)">
  • <dx-form
  • [formData]="employee"
  • [colCount]="2">
  • <dxi-form-item
  • itemType="group"
  • caption="Personal Information"
  • [colCount]="2">
  • <dxi-form-item dataField="name" [isRequired]="true">
  • </dxi-form-item>
  • <dxi-form-item dataField="officeNumber">
  • <dxi-form-validation-rule type="numeric">
  • </dxi-form-validation-rule>
  • </dxi-form-item>
  • <dxi-form-item dataField="email">
  • <dxi-form-validation-rule type="email">
  • </dxi-form-validation-rule>
  • </dxi-form-item>
  • </dxi-form-item>
  • <dxi-form-item
  • itemType="button"
  • [buttonOptions]="submitButtonOptions">
  • </dxi-form-item>
  • </dx-form>
  • </form>
  • import notify from 'devextreme/ui/notify';
  •  
  • // ...
  • export class AppComponent {
  • employee = {
  • // ...
  • }
  •  
  • submitButtonOptions = {
  • text: "Submit the Form",
  • useSubmitBehavior: true
  • }
  •  
  • handleSubmit = function(e: Event) {
  • setTimeout(() => {
  • notify('Submitted', 'success', 2000);
  • }, 1000);
  •  
  • e.preventDefault();
  • }
  • }

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

app.component.html
app.component.ts
  • <form action="/employee-page" id="form-container">
  • <dx-form>
  • <dxi-form-item
  • itemType="button"
  • [buttonOptions]="submitButtonOptions">
  • </dxi-form-item>
  • </dx-form>
  • </form>
  • import notify from 'devextreme/ui/notify';
  •  
  • // ...
  • export class AppComponent {
  • submitButtonOptions = {
  • text: "Submit the Form",
  • onClick: function() {
  • const validationResult = formInstance.validate(); // get Form instance beforehand
  • if (validationResult.isValid)
  • document.getElementById("form-container").submit();
  • else
  • notify('Form is Invalid', 'error', 2000);
  • }
  • };
  • }

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