Vue Common - Data Validation
This guide provides the detailed information on validation capabilities of DevExtreme editors. It describes how to validate a single editor or a group of editors, display the validation summary, perform remote validation, use a custom validation engine, etc.
Validate an Editor Value
Associate a DevExtreme editor with the Validator UI component and specify validationRules to validate the editor. The full list of predefined validation rules is available in the Validation Rules Reference section.
- <template>
- <DxTextBox v-model:value="login" placeholder="Login">
- <DxValidator>
- <DxRequiredRule />
- <DxPatternRule pattern="^[a-zA-Z]+$" message="Do not use digits." />
- </DxValidator>
- </DxTextBox>
- </template>
- <script>
- import 'devextreme/dist/css/dx.light.css';
- import DxTextBox from 'devextreme-vue/text-box';
- import DxValidator, {
- DxRequiredRule,
- DxPatternRule
- } from 'devextreme-vue/validator';
- export default {
- components: {
- DxTextBox,
- DxValidator,
- DxRequiredRule,
- DxPatternRule
- },
- data() {
- return {
- login: undefined
- }
- }
- }
- </script>
Group the Editors
Editors belonging to a single Validation Group can be validated together. All editors on a page are automatically collected in a Default Validation Group, which is suitable when you do not need to validate collections of editors separately. In other cases, define Validation Groups as shown in the following code:
- <template>
- <DxValidationGroup name="loginGroup">
- <DxTextBox v-model:value="login" placeholder="Login">
- <DxValidator>
- <!-- Login validation rules are configured here -->
- </DxValidator>
- </DxTextBox>
- <DxTextBox v-model:value="password" placeholder="Password">
- <DxValidator>
- <!-- Password validation rules are configured here -->
- </DxValidator>
- </DxTextBox>
- </DxValidationGroup>
- </template>
- <script>
- import 'devextreme/dist/css/dx.light.css';
- import DxTextBox from 'devextreme-vue/text-box';
- import DxValidator, {
- DxRequiredRule,
- DxPatternRule
- } from 'devextreme-vue/validator';
- import DxValidationGroup from 'devextreme-vue/validation-group';
- export default {
- components: {
- DxTextBox,
- DxValidator,
- DxRequiredRule,
- DxPatternRule,
- DxValidationGroup
- },
- data() {
- return {
- login: undefined,
- password: undefined
- }
- }
- }
- </script>
Validate the Group
Call a group's validate() method in a Button's onClick event handler to validate the group. You can access the Validation Group via the handler's argument. The Button always validates the group to which it belongs. If the membership is not specified, the Button validates the Default Validation Group.
- <template>
- <!-- <DxValidationGroup> -->
- <DxTextBox v-model:value="login" placeholder="Login">
- <DxValidator>
- <!-- Login validation rules are configured here -->
- </DxValidator>
- </DxTextBox>
- <DxTextBox v-model:value="password" placeholder="Password">
- <DxValidator>
- <!-- Password validation rules are configured here -->
- </DxValidator>
- </DxTextBox>
- <DxButton text="Sign in" @click="signIn" />
- <!-- </DxValidationGroup> -->
- </template>
- <script>
- import 'devextreme/dist/css/dx.light.css';
- import DxTextBox from 'devextreme-vue/text-box';
- import DxValidator, {
- DxRequiredRule,
- DxPatternRule
- } from 'devextreme-vue/validator';
- // import DxValidationGroup from 'devextreme-vue/validation-group';
- import DxButton from 'devextreme-vue/button';
- export default {
- components: {
- DxTextBox,
- DxValidator,
- DxRequiredRule,
- DxPatternRule,
- // DxValidationGroup,
- DxButton
- },
- data() {
- return {
- login: undefined,
- password: undefined
- }
- },
- methods: {
- signIn(e) {
- let result = e.validationGroup.validate();
- if (result.isValid) {
- // Submit values to the server
- }
- }
- }
- }
- </script>
Alternatively, you can get a group's instance and call its validate method to validate this group:
- <template>
- <div>
- <DxValidationGroup
- :ref="groupRefKey">
- <DxTextBox>
- <DxValidator>
- <DxRequiredRule />
- </DxValidator>
- </DxTextBox>
- <DxTextBox>
- <DxValidator>
- <DxRequiredRule />
- </DxValidator>
- </DxTextBox>
- </DxValidationGroup>
- <DxButton
- text="Sign in"
- @click="validateGroup"
- />
- </div>
- </template>
- <script>
- import 'devextreme/dist/css/dx.light.css';
- import DxTextBox from 'devextreme-vue/text-box';
- import DxValidator, { DxRequiredRule } from 'devextreme-vue/validator';
- import DxValidationGroup from 'devextreme-vue/validation-group';
- import DxButton from 'devextreme-vue/button';
- export default {
- components: {
- DxTextBox,
- DxValidator,
- DxRequiredRule,
- DxValidationGroup,
- DxButton
- },
- data() {
- groupRefKey: 'targetGroup'
- },
- methods: {
- validateGroup() {
- this.validationGroup.validate();
- }
- },
- computed: {
- validationGroup: function() {
- return this.$refs[this.groupRefKey].instance;
- }
- }
- }
- </script>
Display Validation Errors
All group validation errors can be displayed in the ValidationSummary UI component. The following code shows how to add this UI component to a page. The commented-out codelines associate the Validation Summary with a named Validation Group.
- <template>
- <!-- <DxValidationGroup name="loginGroup"> -->
- ...
- <DxValidationSummary />
- <!-- </DxValidationGroup> -->
- </template>
- <script>
- // ...
- import DxValidationSummary from 'devextreme-vue/validation-summary';
- // import DxValidationGroup from 'devextreme-vue/validation-group';
- export default {
- components: {
- // ...
- // DxValidationGroup,
- DxValidationSummary
- },
- // ...
- }
- </script>
Disable Validation Dynamically
All the rules, except the CustomRule and AsyncRule, are always applied and cannot be disabled at runtime.
If you need to disable validation dynamically, implement a CustomRule or AsyncRule in which you should simulate the validation logic of a target rule but apply it only under certain conditions.
The following example illustrates this case. A page contains two TextBoxes and a CheckBox. The first TextBox has proper RequiredRule; the second TextBox has a CustomRule that simulates the RequiredRule logic but applies it only when the CheckBox is selected. The reevaluate property is enabled to re-check the TextBox value after the CheckBox value was changed.
- <template>
- <div id="root">
- <DxTextBox>
- <DxValidator>
- <DxRequiredRule />
- </DxValidator>
- </DxTextBox>
- <DxTextBox>
- <DxValidator>
- <DxCustomRule
- message="Required"
- :validation-callback="customCallback"
- :reevaluate="true"
- />
- </DxValidator>
- </DxTextBox>
- <DxButton
- text="Validate group"
- @click="validateGroup"
- />
- <DxCheckBox
- v-model:value="checkBoxValue"
- />
- </div>
- </template>
- <script>
- import 'devextreme/dist/css/dx.light.css';
- import { DxTextBox, DxButton, DxCheckBox } from 'devextreme-vue';
- import {
- DxValidator,
- DxRequiredRule,
- DxCustomRule
- } from 'devextreme-vue/validator';
- export default {
- components: {
- DxTextBox,
- DxButton,
- DxCheckBox,
- DxValidator,
- DxRequiredRule,
- DxCustomRule
- },
- data() {
- return {
- checkBoxValue: false
- };
- },
- methods: {
- customCallback(e) {
- if (this.checkBoxValue) {
- return !!e.value;
- }
- return true;
- },
- validateGroup(params) {
- params.validationGroup.validate();
- }
- }
- };
- </script>
Custom Validation
To implement custom validation, use the CustomRule. Refer to the validationCallback function's description for an example.
Server-Side Validation
To implement server-side validation, use the AsyncRule. Refer to the validationCallback function's description for an example.
Validate a Custom Value
You can use the DevExtreme validation engine to validate a custom value, for example, a non-DevExtreme editor value or a concatenation of several editor values, by configuring the Validator's adapter property. The following example creates two text boxes and a button. A button click checks that at least one of these text boxes is filled. Their values are provided by the getValue function.
- <template>
- <div>
- <div id="contacts" :style="{ border: borderStyle }">
- <DxTextBox
- v-model:value="phone"
- placeholder="Phone"
- @value-changed="revalidate"
- />
- <DxTextBox
- v-model:value="email"
- placeholder="Email"
- @value-changed="revalidate"
- type="email"
- />
- </div>
- <DxValidator :adapter="adapterConfig">
- <DxRequiredRule message="Specify your phone or email." />
- </DxValidator>
- <DxValidationSummary />
- <DxButton text="Contact me" @click="submit" />
- </div>
- </template>
- <script>
- import 'devextreme/dist/css/dx.light.css';
- import DxTextBox from "devextreme-vue/text-box";
- import DxValidator, { DxRequiredRule } from "devextreme-vue/validator";
- import DxButton from "devextreme-vue/button";
- import DxValidationSummary from "devextreme-vue/validation-summary";
- export default {
- components: {
- DxTextBox,
- DxValidator,
- DxRequiredRule,
- DxButton,
- DxValidationSummary,
- },
- data() {
- const callbacks = [];
- const adapterConfig = {
- getValue: () => {
- return this.phone || this.email;
- },
- applyValidationResults: (e) => {
- this.borderStyle = e.isValid ? "none" : "1px solid red";
- },
- validationRequestsCallbacks: callbacks,
- };
- const revalidate = () => {
- callbacks.forEach((func) => {
- func();
- });
- };
- return {
- phone: undefined,
- email: undefined,
- borderStyle: "none",
- adapterConfig,
- revalidate
- };
- },
- methods: {
- submit (e) {
- const { isValid } = e.validationGroup.validate();
- if (isValid) {
- // Submit values to the server
- }
- }
- },
- };
- </script>
Conditional Validation
To conditionally validate an editor, use conditional rendering on a validation rule. In the example below, the TextBox will be validated only if the SelectBox value is Germany
.
Refer to the following article for more information about conditional rendering: Conditional Rendering.
- <template>
- <DxTextBox>
- <DxValidator>
- <DxRequiredRule
- v-if="country === 'Germany'"
- message="Name is required"
- />
- </DxValidator>
- </DxTextBox>
- <DxSelectBox
- :data-source="countries"
- v-model:value="country"
- >
- </DxSelectBox>
- </template>
- <script>
- import DxTextBox from 'devextreme-vue/text-box';
- import DxSelectBox from 'devextreme-vue/select-box';
- import { DxValidator, DxRequiredRule } from 'devextreme-vue/validator';
- export default {
- components: {
- DxTextBox,
- DxSelectBox,
- DxValidator,
- DxRequiredRule
- },
- data() {
- return {
- countries: ['Germany', 'USA', 'China'],
- country: ''
- }
- }
- }
- </script>
If you have technical questions, please create a support ticket in the DevExpress Support Center.