UNPKG

devextreme

Version:

JavaScript/TypeScript Component Suite for Responsive Web Development

310 lines (309 loc) • 12.5 kB
/** * DevExtreme (esm/__internal/ui/validator.js) * Version: 26.1.6 * Build date: Fri Oct 16 2026 * * Copyright (c) 2012 - 2026 Developer Express Inc. ALL RIGHTS RESERVED * Read about DevExtreme licensing here: https://js.devexpress.com/Licensing/ */ import _objectWithoutPropertiesLoose from "@babel/runtime/helpers/esm/objectWithoutPropertiesLoose"; const _excluded = ["id", "complete"]; import registerComponent from "../../core/component_registrator"; import { data as elementData } from "../../core/element_data"; import Guid from "../../core/guid"; import Callbacks from "../../core/utils/callbacks"; import { Deferred } from "../../core/utils/deferred"; import { extend } from "../../core/utils/extend"; import errors from "../../ui/widget/ui.errors"; import DOMComponent from "../core/widget/dom_component"; import DefaultAdapter from "../ui/validation/default_adapter"; import ValidationEngine from "../ui/validation_engine"; const VALIDATOR_CLASS = "dx-validator"; const VALIDATION_STATUS_VALID = "valid"; const VALIDATION_STATUS_INVALID = "invalid"; const VALIDATION_STATUS_PENDING = "pending"; class Validator extends DOMComponent { _initOptions(options) { super._initOptions(options); this.option(ValidationEngine.initValidationOptions(options)) } _getDefaultOptions() { return Object.assign({}, super._getDefaultOptions(), { validationRules: [] }) } _init() { super._init(); this._initGroupRegistration(); this.focused = Callbacks(); this._initAdapter(); this._validationInfo = { result: null, deferred: null, skipValidation: false } } _initGroupRegistration() { const group = this._findGroup(); if (!this._groupWasInit) { this.on("disposing", args => { ValidationEngine.removeRegisteredValidator(args.component._validationGroup, args.component) }) } if (!this._groupWasInit || this._validationGroup !== group) { ValidationEngine.removeRegisteredValidator(this._validationGroup, this); this._groupWasInit = true; this._validationGroup = group; ValidationEngine.registerValidatorInGroup(group, this) } } _setOptionsByReference() { super._setOptionsByReference(); Object.assign(this._optionsByReference, { validationGroup: true }) } _getEditor() { const element = this.$element()[0]; return elementData(element, "dx-validation-target") } _initAdapter() { const dxStandardEditor = this._getEditor(); let { adapter: adapter } = this.option(); if (!adapter) { if (dxStandardEditor) { var _adapter; adapter = new DefaultAdapter(dxStandardEditor, this); null === (_adapter = adapter) || void 0 === _adapter || null === (_adapter = _adapter.validationRequestsCallbacks) || void 0 === _adapter || _adapter.push(args => { var _this$_validationInfo; if (null !== (_this$_validationInfo = this._validationInfo) && void 0 !== _this$_validationInfo && _this$_validationInfo.skipValidation) { return } this.validate(args) }); this.option("adapter", adapter); return } throw errors.Error("E0120") } const callbacks = adapter.validationRequestsCallbacks; if (callbacks) { callbacks.push(args => { this.validate(args) }) } } _toggleRTLDirection(isRtl) { var _adapter$editor; const { adapter: adapter } = this.option(); const editorOptions = (null === adapter || void 0 === adapter || null === (_adapter$editor = adapter.editor) || void 0 === _adapter$editor ? void 0 : _adapter$editor.option()) ?? {}; const rtlEnabled = editorOptions.rtlEnabled ?? isRtl; super._toggleRTLDirection(rtlEnabled) } _initMarkup() { this.$element().addClass("dx-validator"); super._initMarkup() } _render() { super._render(); this._toggleAccessibilityAttributes() } _toggleAccessibilityAttributes() { const dxStandardEditor = this._getEditor(); if (dxStandardEditor) { const { validationRules: validationRules } = this.option() ?? {}; const isRequired = (null === validationRules || void 0 === validationRules ? void 0 : validationRules.some(_ref => { let { type: type } = _ref; return "required" === type })) || null; if (dxStandardEditor.isInitialized()) { dxStandardEditor.setAria("required", isRequired) } dxStandardEditor.option("_onMarkupRendered", () => { dxStandardEditor.setAria("required", isRequired) }) } } _visibilityChanged(visible) { if (visible) { this._initGroupRegistration() } } _optionChanged(args) { switch (args.name) { case "validationGroup": this._initGroupRegistration(); return; case "validationRules": { this._resetValidationRules(); this._toggleAccessibilityAttributes(); const { isValid: isValid } = this.option(); if (void 0 !== isValid) { this.validate() } return } case "adapter": this._initAdapter(); break; case "isValid": case "validationStatus": this.option(ValidationEngine.synchronizeValidationOptions(args, this.option())); break; default: super._optionChanged(args) } } _getValidationRules() { const { validationRules: validationRules } = this.option(); this._validationRules ?? (this._validationRules = null === validationRules || void 0 === validationRules ? void 0 : validationRules.map((rule, index) => Object.assign({}, rule, { validator: this, index: index }))); return this._validationRules ?? [] } _findGroup() { const $element = this.$element(); const { validationGroup: validationGroup } = this.option(); return validationGroup || ValidationEngine.findGroup($element, this._modelByElement($element)) } _resetValidationRules() { delete this._validationRules } validate(args) { var _adapter$bypass, _adapter$getValue, _adapter$getCurrentVa, _this$_validationInfo2, _result; const { adapter: adapter, name: name } = this.option(); const bypass = null === adapter || void 0 === adapter || null === (_adapter$bypass = adapter.bypass) || void 0 === _adapter$bypass ? void 0 : _adapter$bypass.call(adapter); const value = (null === args || void 0 === args ? void 0 : args.value) ?? (null === adapter || void 0 === adapter || null === (_adapter$getValue = adapter.getValue) || void 0 === _adapter$getValue ? void 0 : _adapter$getValue.call(adapter)); const currentError = null === adapter || void 0 === adapter || null === (_adapter$getCurrentVa = adapter.getCurrentValidationError) || void 0 === _adapter$getCurrentVa ? void 0 : _adapter$getCurrentVa.call(adapter); const rules = this._getValidationRules(); const currentResult = null === (_this$_validationInfo2 = this._validationInfo) || void 0 === _this$_validationInfo2 ? void 0 : _this$_validationInfo2.result; if ("pending" === (null === currentResult || void 0 === currentResult ? void 0 : currentResult.status) && currentResult.value === value) { return extend({}, currentResult) } let result; if (bypass) { result = { isValid: true, status: "valid" } } else if (null !== currentError && void 0 !== currentError && currentError.editorSpecific) { currentError.validator = this; result = { isValid: false, status: "invalid", brokenRule: currentError, brokenRules: [currentError] } } else { result = ValidationEngine.validate(value, rules, name) } result.id = (new Guid).toString(); this._applyValidationResult(result, adapter); null === (_result = result) || void 0 === _result || null === (_result = _result.complete) || void 0 === _result || _result.then(res => { var _this$_validationInfo3; if (res.id === (null === (_this$_validationInfo3 = this._validationInfo.result) || void 0 === _this$_validationInfo3 ? void 0 : _this$_validationInfo3.id)) { this._applyValidationResult(res, adapter) } }); return extend({}, this._validationInfo.result) } reset() { var _adapter$reset; const { adapter: adapter } = this.option(); const result = { id: null, isValid: true, brokenRule: null, brokenRules: null, pendingRules: null, status: "valid", complete: null }; this._validationInfo.skipValidation = true; null === adapter || void 0 === adapter || null === (_adapter$reset = adapter.reset) || void 0 === _adapter$reset || _adapter$reset.call(adapter); this._validationInfo.skipValidation = false; this._resetValidationRules(); this._applyValidationResult(result, adapter) } _updateValidationResult(result) { const { result: currentResult } = this._validationInfo; if (!currentResult || currentResult.id !== result.id) { const complete = this._validationInfo.deferred && (null === currentResult || void 0 === currentResult ? void 0 : currentResult.complete); this._validationInfo.result = extend({}, result, { complete: complete }) } else { const restResultProperties = _objectWithoutPropertiesLoose(result, _excluded); Object.assign(currentResult, restResultProperties) } } _applyValidationResult(result, adapter) { const validatedAction = this._createActionByOption("onValidated", { excludeValidators: ["readOnly"] }); result.validator = this; this._updateValidationResult(result); const { result: currentResult } = this._validationInfo; if (currentResult && "function" === typeof(null === adapter || void 0 === adapter ? void 0 : adapter.applyValidationResults)) { adapter.applyValidationResults(currentResult) } this.option({ validationStatus: null === currentResult || void 0 === currentResult ? void 0 : currentResult.status }); if ("pending" === (null === currentResult || void 0 === currentResult ? void 0 : currentResult.status)) { if (!this._validationInfo.deferred) { this._validationInfo.deferred = Deferred(); currentResult.complete = this._validationInfo.deferred.promise() } this._eventsStrategy.fireEvent("validating", [currentResult]); return } validatedAction(result); if (this._validationInfo.deferred) { this._validationInfo.deferred.resolve(result); this._validationInfo.deferred = null } } focus() { var _adapter$focus; const { adapter: adapter } = this.option(); null === adapter || void 0 === adapter || null === (_adapter$focus = adapter.focus) || void 0 === _adapter$focus || _adapter$focus.call(adapter) } _useTemplates() { return false } } registerComponent("dxValidator", Validator); export default Validator;