DevExtreme Vue - Validate and Submit the Form

The Form widget uses the built-in validation engine to validate form item values. You can attach validation rules to a simple item using its validationRules option when you create items explicitly...

jQuery
JavaScript
$(function() {
    $("#formContainer").dxForm({
        formData: {
            firstName: "John",
            lastName: "Heart"
        },
        items: [{
            dataField: "firstName",
            validationRules: [{
                type: "required",
                message: "First Name is required"
            }, {
                type: "pattern",
                pattern: "^[a-zA-Z]+$",
                message: "The name should not contain digits"
            }]
        },
        // ...
        ]
    });
});
Angular
HTML
TypeScript
<dx-form [(formData)]="employee">
    <dxi-item dataField="firstName">
        <dxi-validation-rule
            type="required"
            message="First Name is required">
        </dxi-validation-rule>
        <dxi-validation-rule
            type="pattern"
            pattern="^[a-zA-Z]+$"
            message="The name should not contain digits">
        </dxi-validation-rule>
    </dxi-item>
    <!-- ... -->
</dx-form>
import { DxFormModule } from "devextreme-angular";
// ...
export class AppComponent {
    employee = {
        firstName: "John",
        lastName: "Heart"
    }
}
@NgModule({
    imports: [
        // ...
        DxFormModule
    ],
    // ...
})

... or when you customize automatically generated items.

jQuery
JavaScript
$(function() {
    $("#formContainer").dxForm({
        formData: {
            firstName: "John",
            lastName: "Heart"
        },
        customizeItem: function(item) {
            if(item.dataField === "FirstName" || item.dataField === "LastName") {
                item.validationRules = [{
                    type: "required",
                    message: "The value is required"
                }, {
                    type: "pattern",
                    pattern: "^[a-zA-Z]+$",
                    message: "The value should not contain digits"
                }]
            }
        }
    });
});
Angular
HTML
TypeScript
<dx-form
    [(formData)]="employee"
    [customizeItem]="form_customizeItem">
</dx-form>
import { DxFormModule } from "devextreme-angular";
// ...
export class AppComponent {
    employee = {
        firstName: "John",
        lastName: "Heart"
    }
    form_customizeItem (item) {
        if (item.dataField === "FirstName" || item.dataField === "LastName") {
            item.validationRules = [{
                type: "required",
                message: "The value is required"
            }, {
                type: "pattern",
                pattern: "^[a-zA-Z]+$",
                message: "The value should not contain digits"
            }]
        }
    }
}
@NgModule({
    imports: [
        // ...
        DxFormModule
    ],
    // ...
})
NOTE
The RequiredRule is attached to a form item implicitly if this item's isRequired option is set to true.

A single Form editor is validated individually once its value changes. If the value fails to pass the validation check, the editor displays an error message. Note that you can also call the validate() method to validate all Form editors simultaneously. In this case, the Form can display all validation errors at the bottom if you set the showValidationSummary option to true.

Usually, Form editors should be submitted to the server after being successfully validated on the client. The following code shows how to do this using a button form item. Note that the Form widget is wrapped in the <form> tag in the markup.

jQuery
HTML
JavaScript
<form action="/Login" method="post">
    <div id="formWidget"></div>
</form>
$(function () {
    $("#formWidget").dxForm({
        // ...
        validationGroup: "groupName",
        items: [{
            itemType: "button",
            buttonOptions: {
                text: "Submit the Form",
                useSubmitBehavior: true
            }
        },
        // ...
        ]
    });
});
Angular
HTML
TypeScript
<form action="/Login" method="post">
    <dx-form ...
        validationGroup="groupName">
        <dxi-item 
            itemType="button"
            [buttonOptions]="buttonOptions">
        </dxi-item>
    </dx-form>
</form>
import { DxFormModule } from "devextreme-angular";
// ...
export class AppComponent {
    buttonOptions = {
        text: "Submit the Form",
        useSubmitBehavior: true
    }
}
@NgModule({
    imports: [
        // ...
        DxFormModule
    ],
    // ...
})
See Also