validator.js 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. /**
  2. * DevExtreme (ui/validator.js)
  3. * Version: 19.1.16
  4. * Build date: Tue Oct 18 2022
  5. *
  6. * Copyright (c) 2012 - 2022 Developer Express Inc. ALL RIGHTS RESERVED
  7. * Read about DevExtreme licensing here: https://js.devexpress.com/Licensing/
  8. */
  9. "use strict";
  10. var dataUtils = require("../core/element_data");
  11. var Callbacks = require("../core/utils/callbacks");
  12. var errors = require("./widget/ui.errors");
  13. var DOMComponent = require("../core/dom_component");
  14. var extend = require("../core/utils/extend").extend;
  15. var map = require("../core/utils/iterator").map;
  16. var ValidationMixin = require("./validation/validation_mixin");
  17. var ValidationEngine = require("./validation_engine");
  18. var DefaultAdapter = require("./validation/default_adapter");
  19. var registerComponent = require("../core/component_registrator");
  20. var VALIDATOR_CLASS = "dx-validator";
  21. var Validator = DOMComponent.inherit({
  22. _getDefaultOptions: function() {
  23. return extend(this.callBase(), {
  24. validationRules: []
  25. })
  26. },
  27. _init: function() {
  28. this.callBase();
  29. this._initGroupRegistration();
  30. this._skipValidation = false;
  31. this.focused = Callbacks();
  32. this._initAdapter()
  33. },
  34. _initGroupRegistration: function() {
  35. var group = this._findGroup();
  36. if (!this._groupWasInit) {
  37. this.on("disposing", function(args) {
  38. ValidationEngine.removeRegisteredValidator(args.component._validationGroup, args.component)
  39. })
  40. }
  41. if (!this._groupWasInit || this._validationGroup !== group) {
  42. ValidationEngine.removeRegisteredValidator(this._validationGroup, this);
  43. this._groupWasInit = true;
  44. this._validationGroup = group;
  45. ValidationEngine.registerValidatorInGroup(group, this)
  46. }
  47. },
  48. _setOptionsByReference: function() {
  49. this.callBase();
  50. extend(this._optionsByReference, {
  51. validationGroup: true
  52. })
  53. },
  54. _initAdapter: function() {
  55. var that = this;
  56. var element = that.$element()[0];
  57. var dxStandardEditor = dataUtils.data(element, "dx-validation-target");
  58. var adapter = that.option("adapter");
  59. if (!adapter) {
  60. if (dxStandardEditor) {
  61. adapter = new DefaultAdapter(dxStandardEditor, this);
  62. adapter.validationRequestsCallbacks.add(function(args) {
  63. if (that._skipValidation) {
  64. return
  65. }
  66. that.validate(args)
  67. });
  68. this.option("adapter", adapter);
  69. return
  70. }
  71. throw errors.Error("E0120")
  72. }
  73. var callbacks = adapter.validationRequestsCallbacks;
  74. if (callbacks) {
  75. if (Array.isArray(callbacks)) {
  76. callbacks.push(function(args) {
  77. that.validate(args)
  78. })
  79. } else {
  80. errors.log("W0014", "validationRequestsCallbacks", "jQuery.Callbacks", "17.2", "Use the array instead");
  81. callbacks.add(function(args) {
  82. that.validate(args)
  83. })
  84. }
  85. }
  86. },
  87. _initMarkup: function() {
  88. this.$element().addClass(VALIDATOR_CLASS);
  89. this.callBase()
  90. },
  91. _toggleRTLDirection: function(isRtl) {
  92. var _this$option$editor$o, _this$option, _this$option$editor;
  93. var rtlEnabled = null !== (_this$option$editor$o = null === (_this$option = this.option("adapter")) || void 0 === _this$option ? void 0 : null === (_this$option$editor = _this$option.editor) || void 0 === _this$option$editor ? void 0 : _this$option$editor.option("rtlEnabled")) && void 0 !== _this$option$editor$o ? _this$option$editor$o : isRtl;
  94. this.callBase(rtlEnabled)
  95. },
  96. _visibilityChanged: function(visible) {
  97. if (visible) {
  98. this._initGroupRegistration()
  99. }
  100. },
  101. _optionChanged: function(args) {
  102. switch (args.name) {
  103. case "validationGroup":
  104. this._initGroupRegistration();
  105. return;
  106. case "validationRules":
  107. this._resetValidationRules();
  108. void 0 !== this.option("isValid") && this.validate();
  109. return;
  110. case "adapter":
  111. this._initAdapter();
  112. break;
  113. default:
  114. this.callBase(args)
  115. }
  116. },
  117. _getValidationRules: function() {
  118. if (!this._validationRules) {
  119. this._validationRules = map(this.option("validationRules"), function(rule) {
  120. return extend({}, rule, {
  121. validator: this
  122. })
  123. }.bind(this))
  124. }
  125. return this._validationRules
  126. },
  127. _resetValidationRules: function() {
  128. delete this._validationRules
  129. },
  130. validate: function(args) {
  131. var that = this;
  132. var adapter = that.option("adapter");
  133. var name = that.option("name");
  134. var bypass = adapter.bypass && adapter.bypass();
  135. var value = args && void 0 !== args.value ? args.value : adapter.getValue();
  136. var currentError = adapter.getCurrentValidationError && adapter.getCurrentValidationError();
  137. var rules = this._getValidationRules();
  138. var result;
  139. if (bypass) {
  140. result = {
  141. isValid: true
  142. }
  143. } else {
  144. if (currentError && currentError.editorSpecific) {
  145. currentError.validator = this;
  146. result = {
  147. isValid: false,
  148. brokenRule: currentError
  149. }
  150. } else {
  151. result = ValidationEngine.validate(value, rules, name)
  152. }
  153. }
  154. this._applyValidationResult(result, adapter);
  155. return result
  156. },
  157. reset: function() {
  158. var that = this;
  159. var adapter = that.option("adapter");
  160. var result = {
  161. isValid: true,
  162. brokenRule: null
  163. };
  164. this._skipValidation = true;
  165. adapter.reset();
  166. this._skipValidation = false;
  167. this._resetValidationRules();
  168. this._applyValidationResult(result, adapter)
  169. },
  170. _applyValidationResult: function(result, adapter) {
  171. var validatedAction = this._createActionByOption("onValidated", {
  172. excludeValidators: ["readOnly"]
  173. });
  174. result.validator = this;
  175. adapter.applyValidationResults && adapter.applyValidationResults(result);
  176. this.option({
  177. isValid: result.isValid
  178. });
  179. validatedAction(result)
  180. },
  181. focus: function() {
  182. var adapter = this.option("adapter");
  183. adapter && adapter.focus && adapter.focus()
  184. }
  185. }).include(ValidationMixin);
  186. registerComponent("dxValidator", Validator);
  187. module.exports = Validator;
  188. module.exports.default = module.exports;