UNPKG

devextreme

Version:

JavaScript/TypeScript Component Suite for Responsive Web Development

329 lines (326 loc) • 13.3 kB
/** * DevExtreme (cjs/__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/ */ "use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.default = void 0; var _component_registrator = _interopRequireDefault(require("../../core/component_registrator")); var _element_data = require("../../core/element_data"); var _guid = _interopRequireDefault(require("../../core/guid")); var _callbacks = _interopRequireDefault(require("../../core/utils/callbacks")); var _deferred = require("../../core/utils/deferred"); var _extend = require("../../core/utils/extend"); var _ui = _interopRequireDefault(require("../../ui/widget/ui.errors")); var _dom_component = _interopRequireDefault(require("../core/widget/dom_component")); var _default_adapter = _interopRequireDefault(require("../ui/validation/default_adapter")); var _validation_engine = _interopRequireDefault(require("../ui/validation_engine")); const _excluded = ["id", "complete"]; function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e } } function _objectWithoutPropertiesLoose(r, e) { if (null == r) { return {} } var t = {}; for (var n in r) { if ({}.hasOwnProperty.call(r, n)) { if (-1 !== e.indexOf(n)) { continue } t[n] = r[n] } } return t } const VALIDATOR_CLASS = "dx-validator"; const VALIDATION_STATUS_VALID = "valid"; const VALIDATION_STATUS_INVALID = "invalid"; const VALIDATION_STATUS_PENDING = "pending"; class Validator extends _dom_component.default { _initOptions(options) { super._initOptions(options); this.option(_validation_engine.default.initValidationOptions(options)) } _getDefaultOptions() { return Object.assign({}, super._getDefaultOptions(), { validationRules: [] }) } _init() { super._init(); this._initGroupRegistration(); this.focused = (0, _callbacks.default)(); this._initAdapter(); this._validationInfo = { result: null, deferred: null, skipValidation: false } } _initGroupRegistration() { const group = this._findGroup(); if (!this._groupWasInit) { this.on("disposing", args => { _validation_engine.default.removeRegisteredValidator(args.component._validationGroup, args.component) }) } if (!this._groupWasInit || this._validationGroup !== group) { _validation_engine.default.removeRegisteredValidator(this._validationGroup, this); this._groupWasInit = true; this._validationGroup = group; _validation_engine.default.registerValidatorInGroup(group, this) } } _setOptionsByReference() { super._setOptionsByReference(); Object.assign(this._optionsByReference, { validationGroup: true }) } _getEditor() { const element = this.$element()[0]; return (0, _element_data.data)(element, "dx-validation-target") } _initAdapter() { const dxStandardEditor = this._getEditor(); let { adapter: adapter } = this.option(); if (!adapter) { if (dxStandardEditor) { var _adapter; adapter = new _default_adapter.default(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 _ui.default.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(_validation_engine.default.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 || _validation_engine.default.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 (0, _extend.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 = _validation_engine.default.validate(value, rules, name) } result.id = (new _guid.default).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 (0, _extend.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 = (0, _extend.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 = (0, _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 } }(0, _component_registrator.default)("dxValidator", Validator); var _default = exports.default = Validator;