@daracl/form 0.0.4 → 0.0.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (68) hide show
  1. package/dist/193.daracl.form.min.js +7 -0
  2. package/dist/193.daracl.form.min.js.LICENSE.txt +5 -0
  3. package/dist/193.daracl.form.min.js.map +1 -0
  4. package/dist/dara.datetimepicker.js +304 -0
  5. package/dist/dara.datetimepicker.js.LICENSE.txt +5 -0
  6. package/dist/dara.datetimepicker.js.map +1 -0
  7. package/dist/daracl.form.js +3214 -5940
  8. package/dist/daracl.form.js.map +1 -1
  9. package/dist/daracl.form.min.css +2 -2
  10. package/dist/daracl.form.min.css.map +1 -1
  11. package/dist/daracl.form.min.js +2 -2
  12. package/dist/daracl.form.min.js.LICENSE.txt +1 -1
  13. package/dist/daracl.form.min.js.map +1 -1
  14. package/dist/daracl.vender.js +2822 -0
  15. package/dist/daracl.vender.js.LICENSE.txt +5 -0
  16. package/dist/daracl.vender.js.map +1 -0
  17. package/dist/index.cjs +5 -1
  18. package/dist/index.cjs.map +2 -2
  19. package/dist/index.css.map +2 -2
  20. package/dist/index.html +1 -1
  21. package/dist/index.js +5 -1
  22. package/dist/index.js.map +2 -2
  23. package/dist/util/utils.d.ts +1 -1
  24. package/dist/vendors-node_modules_daracl_datetimepicker_dist_index_js.daracl.form.js +1477 -0
  25. package/dist/vendors-node_modules_daracl_datetimepicker_dist_index_js.daracl.form.js.map +1 -0
  26. package/package.json +11 -7
  27. package/src/DaraForm.ts +431 -0
  28. package/src/FieldInfoMap.ts +356 -0
  29. package/src/FormTemplate.ts +207 -0
  30. package/src/constants.ts +100 -0
  31. package/src/event/renderEvents.ts +41 -0
  32. package/src/index copy.html +591 -0
  33. package/src/index-grid.html +655 -0
  34. package/src/index-tab.html +639 -0
  35. package/src/index.html +483 -0
  36. package/src/renderer/ButtonRender.ts +51 -0
  37. package/src/renderer/CheckboxRender.ts +214 -0
  38. package/src/renderer/CustomRender.ts +100 -0
  39. package/src/renderer/DateRender.ts +104 -0
  40. package/src/renderer/DropdownRender.ts +149 -0
  41. package/src/renderer/FileRender.ts +239 -0
  42. package/src/renderer/GridRender.ts +289 -0
  43. package/src/renderer/GroupRender.ts +47 -0
  44. package/src/renderer/HiddenRender.ts +36 -0
  45. package/src/renderer/NumberRender.ts +68 -0
  46. package/src/renderer/PasswordRender.ts +69 -0
  47. package/src/renderer/RadioRender.ts +191 -0
  48. package/src/renderer/RangeRender.ts +79 -0
  49. package/src/renderer/Render.ts +194 -0
  50. package/src/renderer/TabRender.ts +229 -0
  51. package/src/renderer/TextAreaRender.ts +72 -0
  52. package/src/renderer/TextRender.ts +68 -0
  53. package/src/rule/fileValidator.ts +26 -0
  54. package/src/rule/numberValidator.ts +96 -0
  55. package/src/rule/regexpValidator.ts +45 -0
  56. package/src/rule/stringValidator.ts +64 -0
  57. package/src/rule/validator.ts +49 -0
  58. package/src/types/DataMap.d.ts +17 -0
  59. package/src/types/FormField.d.ts +129 -0
  60. package/src/types/FormOptions.d.ts +91 -0
  61. package/src/types/Message.d.ts +40 -0
  62. package/src/types/ValidResult.d.ts +9 -0
  63. package/src/util/Lanauage.ts +127 -0
  64. package/src/util/renderFactory.ts +26 -0
  65. package/src/util/styleUtils.ts +85 -0
  66. package/src/util/utils.ts +283 -0
  67. package/src/util/validUtils.ts +53 -0
  68. package/style/daracl.form.scss +2 -1
@@ -0,0 +1,229 @@
1
+ import { FieldStyle, FormField } from "@t/FormField";
2
+ import Render from "./Render";
3
+ import { invalidMessage } from "src/util/validUtils";
4
+ import { stringValidator } from "src/rule/stringValidator";
5
+ import DaraForm from "src/DaraForm";
6
+ import FormTemplate from "src/FormTemplate";
7
+ import styleUtils from "src/util/styleUtils";
8
+ import { FormOptions } from "@t/FormOptions";
9
+ import * as utils from "src/util/utils";
10
+
11
+ export default class TabRender extends Render {
12
+ private customFunction;
13
+ private tabContainerElement: HTMLElement;
14
+ constructor(field: FormField, rowElement: HTMLElement, daraForm: DaraForm) {
15
+ super(daraForm, field, rowElement);
16
+
17
+ if (field.renderer) {
18
+ this.customFunction = field.renderer;
19
+ } else {
20
+ this.customFunction = {} as any;
21
+ }
22
+
23
+ this.mounted();
24
+ }
25
+
26
+ mounted() {
27
+ if (this.customFunction.mounted) {
28
+ (this.customFunction.mounted as any).call(this, this.field, this.rowElement);
29
+ }
30
+
31
+ this.tabContainerElement.querySelectorAll(".tab-item").forEach((tabItem) => {
32
+ tabItem.addEventListener("click", (e: any) => {
33
+ this.clickEventHandler(tabItem, e);
34
+ });
35
+ });
36
+ }
37
+
38
+ /**
39
+ * tab item click
40
+ *
41
+ * @param {Element} tabItem
42
+ * @param {*} evt
43
+ */
44
+ private clickEventHandler(tabItem: Element, evt: any) {
45
+ this.setActive(tabItem.getAttribute("data-tab-id") ?? "");
46
+ }
47
+
48
+ public setActive(tabId: string): void {
49
+ const tabItem = this.tabContainerElement.querySelector(`[data-tab-id="${tabId}"]`);
50
+
51
+ if (!tabItem) return;
52
+
53
+ if (!tabItem.classList.contains("active")) {
54
+ for (let item of tabItem?.parentElement?.children ?? []) {
55
+ item.classList.remove("active");
56
+ }
57
+ tabItem.classList.add("active");
58
+ }
59
+
60
+ const tabPanel = this.rowElement.querySelector(`[tab-panel-id="${tabId}"]`);
61
+
62
+ if (!tabPanel?.classList.contains("active")) {
63
+ for (let item of tabPanel?.parentElement?.children ?? []) {
64
+ item.classList.remove("active");
65
+ }
66
+
67
+ tabPanel?.classList.add("active");
68
+ }
69
+ }
70
+
71
+ static isDataRender(): boolean {
72
+ return false;
73
+ }
74
+
75
+ static isChildrenCreate(): boolean {
76
+ return false;
77
+ }
78
+
79
+ /**
80
+ * tab template
81
+ *
82
+ * @param {FormField} field
83
+ * @param {FormTemplate} formTemplate
84
+ * @param {FormOptions} options
85
+ * @returns {string} template string
86
+ */
87
+
88
+ createField() {
89
+ const field = this.field;
90
+
91
+ const formTemplate = this.daraForm.formTemplate;
92
+ const options = this.daraForm.getOptions();
93
+
94
+ const fieldContainerElement = this.rowElement.querySelector(".df-field-container") as HTMLElement;
95
+
96
+ //tab 이벤트 처리 할것.
97
+ let fieldStyle = styleUtils.fieldStyle(options, field);
98
+
99
+ fieldContainerElement.innerHTML = `
100
+ <div class="df-field ">
101
+ <div class="tab-header ${fieldStyle.tabAlignClass}"></div>
102
+ </div>
103
+ <div class="df-tab-body"></div>
104
+ `;
105
+
106
+ if (field.children) {
107
+ let firstFlag = true;
108
+
109
+ const formOptions = this.getForm().getOptions();
110
+
111
+ let fieldStyle: FieldStyle = styleUtils.fieldStyle(formOptions, field, null, formTemplate.isLabelHide(field));
112
+
113
+ for (const childField of field.children) {
114
+ childField.$parent = field;
115
+ formTemplate.addRowFieldInfo(childField);
116
+ let id = childField.$key;
117
+
118
+ let tabTemplate = `<span class="tab-item ${firstFlag ? "active" : ""}" data-tab-id="${id}"><a href="javascript:;">${childField.label}</a></span>`;
119
+
120
+ let tabBodyTemplate = `<div class="tab-panel ${firstFlag ? "active" : ""}" tab-panel-id="${id}">${Render.getDescriptionTemplate(childField)}
121
+ <div class="tab-group"></div>
122
+ </div>`;
123
+
124
+ const tabElement = utils.templateToElement(tabTemplate);
125
+ const tabBodyElement = utils.templateToElement(tabBodyTemplate);
126
+
127
+ firstFlag = false;
128
+ if (tabElement) {
129
+ fieldContainerElement.querySelector(".tab-header")?.appendChild(tabElement);
130
+ }
131
+
132
+ if (tabBodyElement) {
133
+ fieldContainerElement.querySelector(".df-tab-body")?.appendChild(tabBodyElement);
134
+
135
+ if (childField.children) {
136
+ let tabFormOptions = {
137
+ style: {
138
+ width: formOptions.style.width ?? "100%",
139
+ labelWidth: formOptions.style.labelWidth ?? 3,
140
+ valueWidth: formOptions.style.valueWidth ?? 9,
141
+ position: formOptions.style.position ?? "left-right",
142
+ },
143
+ useTypeValue: formOptions.useTypeValue,
144
+ autoCreate: false,
145
+ notValidMessage: "This form is not valid.",
146
+ fields: [],
147
+ } as FormOptions;
148
+
149
+ childField.$tabForm = new DaraForm(document.createElement("div"), tabFormOptions);
150
+
151
+ childField.$instance = new (childField.$renderType as any)(childField, tabBodyElement?.querySelector(".tab-group"), childField.$tabForm);
152
+
153
+ childField.$tabForm.formTemplate.childTemplate(childField, fieldStyle);
154
+ }
155
+ }
156
+ }
157
+
158
+ this.tabContainerElement = fieldContainerElement;
159
+ }
160
+ }
161
+
162
+ getValue(): any {
163
+ if (this.customFunction.getValue) {
164
+ return (this.customFunction.getValue as any).call(this, this.field, this.rowElement);
165
+ }
166
+
167
+ let allTabValue = {} as any;
168
+
169
+ let useTypeValue = this.getForm().getOptions().useTypeValue;
170
+
171
+ for (const childField of this.field.children) {
172
+ const childFieldValue = childField.$tabForm?.getValue(false);
173
+ if (useTypeValue) {
174
+ allTabValue[childField.$validName] = childFieldValue;
175
+ } else {
176
+ for (let [key, value] of Object.entries(childFieldValue)) {
177
+ allTabValue[key] = value;
178
+ }
179
+ }
180
+ }
181
+ return allTabValue;
182
+ }
183
+
184
+ setValue(value: any): void {
185
+ for (const childField of this.field.children) {
186
+ let tabChildValue = value[childField.$validName];
187
+
188
+ if (tabChildValue) {
189
+ childField.$tabForm?.setValue(tabChildValue);
190
+ }
191
+ }
192
+ }
193
+
194
+ reset() {}
195
+
196
+ getElement(): Element {
197
+ return this.rowElement;
198
+ }
199
+
200
+ valid(): any {
201
+ if (this.customFunction.valid) {
202
+ const validResult = (this.customFunction.valid as any).call(this, this.field, this.rowElement);
203
+
204
+ invalidMessage(this.field, this.rowElement, validResult);
205
+
206
+ return;
207
+ }
208
+
209
+ const field = this.field;
210
+
211
+ for (const childField of field.children) {
212
+ const validResult = childField.$tabForm?.validForm();
213
+
214
+ if (validResult && validResult.length > 0) {
215
+ const firstItem = validResult[0];
216
+
217
+ if (firstItem.message) {
218
+ firstItem.message = "[" + this.field.label + "] " + firstItem.message;
219
+
220
+ return firstItem;
221
+ }
222
+ }
223
+ }
224
+
225
+ return true;
226
+ }
227
+
228
+ focus(): void {}
229
+ }
@@ -0,0 +1,72 @@
1
+ import { FormField } from "@t/FormField";
2
+ import Render from "./Render";
3
+ import { stringValidator } from "src/rule/stringValidator";
4
+ import { resetRowElementStyleClass, invalidMessage } from "src/util/validUtils";
5
+ import { inputEvent } from "src/event/renderEvents";
6
+ import DaraForm from "src/DaraForm";
7
+
8
+ export default class TextAreaRender extends Render {
9
+ private element: HTMLTextAreaElement;
10
+
11
+ constructor(field: FormField, rowElement: HTMLElement, daraForm: DaraForm) {
12
+ super(daraForm, field, rowElement);
13
+
14
+ this.mounted();
15
+ this.setDefaultOption();
16
+ this.setDefaultInfo();
17
+ }
18
+
19
+ mounted() {
20
+ inputEvent(this.field, this.element, this);
21
+ }
22
+
23
+ createField() {
24
+ const field = this.field;
25
+
26
+ const fieldContainerElement = this.rowElement.querySelector(".df-field-container") as HTMLElement;
27
+
28
+ let rows = field.customOptions?.rows;
29
+ rows = +rows > 0 ? rows : 3;
30
+
31
+ fieldContainerElement.innerHTML = `
32
+ <div class="df-field">
33
+ <textarea name="${field.$xssName}" rows="${rows}" class="form-field textarea help-icon"></textarea>
34
+ </div>
35
+ ${Render.getDescriptionTemplate(field)}
36
+ <div class="help-message"></div>
37
+ `;
38
+
39
+ this.element = fieldContainerElement.querySelector(`[name="${field.$xssName}"]`) as HTMLTextAreaElement;
40
+ }
41
+
42
+ getValue() {
43
+ return this.element.value;
44
+ }
45
+
46
+ setValue(value: any, changeCheckFlag?: boolean): void {
47
+ if (changeCheckFlag !== false && this.changeEventCall(this.field, null, this, value) === false) {
48
+ this.element.value = this.field.$value;
49
+ return;
50
+ }
51
+ this.field.$value = value;
52
+ this.element.value = value;
53
+ }
54
+
55
+ reset() {
56
+ this.setDefaultInfo();
57
+ this.setDisabled(false);
58
+ resetRowElementStyleClass(this.rowElement);
59
+ }
60
+
61
+ getElement(): HTMLElement {
62
+ return this.element;
63
+ }
64
+
65
+ valid(): any {
66
+ const validResult = stringValidator(this.getValue(), this.field);
67
+
68
+ invalidMessage(this.field, this.rowElement, validResult);
69
+
70
+ return validResult;
71
+ }
72
+ }
@@ -0,0 +1,68 @@
1
+ import { FormField } from "@t/FormField";
2
+ import Render from "./Render";
3
+ import { stringValidator } from "src/rule/stringValidator";
4
+ import { resetRowElementStyleClass, invalidMessage } from "src/util/validUtils";
5
+ import { inputEvent } from "src/event/renderEvents";
6
+ import DaraForm from "src/DaraForm";
7
+
8
+ export default class TextRender extends Render {
9
+ private element: HTMLInputElement;
10
+
11
+ constructor(field: FormField, rowElement: HTMLElement, daraForm: DaraForm) {
12
+ super(daraForm, field, rowElement);
13
+ this.mounted();
14
+ this.setDefaultOption();
15
+ this.setDefaultInfo();
16
+ }
17
+
18
+ mounted() {
19
+ inputEvent(this.field, this.element, this);
20
+ }
21
+
22
+ createField() {
23
+ const field = this.field;
24
+
25
+ const fieldContainerElement = this.rowElement.querySelector(".df-field-container") as HTMLElement;
26
+
27
+ fieldContainerElement.innerHTML = `
28
+ <div class="df-field">
29
+ <input type="text" name="${field.$xssName}" class="form-field text help-icon" />
30
+ </div>
31
+ ${Render.getDescriptionTemplate(field)}
32
+ <div class="help-message"></div>
33
+ `;
34
+
35
+ this.element = fieldContainerElement.querySelector(`[name="${field.$xssName}"]`) as HTMLInputElement;
36
+ }
37
+
38
+ getValue() {
39
+ return this.element.value;
40
+ }
41
+
42
+ setValue(value: any, changeCheckFlag?: boolean): void {
43
+ if (changeCheckFlag !== false && this.changeEventCall(this.field, null, this, value) === false) {
44
+ this.element.value = this.field.$value;
45
+ return;
46
+ }
47
+ this.field.$value = value;
48
+ this.element.value = value;
49
+ }
50
+
51
+ reset() {
52
+ this.setDefaultInfo();
53
+ this.setDisabled(false);
54
+ resetRowElementStyleClass(this.rowElement);
55
+ }
56
+
57
+ getElement(): HTMLInputElement {
58
+ return this.element;
59
+ }
60
+
61
+ valid(): any {
62
+ const validResult = stringValidator(this.getValue(), this.field);
63
+
64
+ invalidMessage(this.field, this.rowElement, validResult);
65
+
66
+ return validResult;
67
+ }
68
+ }
@@ -0,0 +1,26 @@
1
+ import { FileInfo, FormField } from "@t/FormField";
2
+ import { ValidResult } from "@t/ValidResult";
3
+ import { RULES } from "src/constants";
4
+
5
+ /**
6
+ * file validator
7
+ *
8
+ * @param {HTMLInputElement} element
9
+ * @param {FormField} field
10
+ * @param {FileInfo[]} fileList
11
+ * @returns {(ValidResult | boolean)}
12
+ */
13
+ export const fileValidator = (element: HTMLInputElement, field: FormField, fileList: FileInfo[]): ValidResult | boolean => {
14
+ const result: ValidResult = { name: field.name, constraint: [] };
15
+
16
+ if (field.required && fileList.length < 1) {
17
+ result.constraint.push(RULES.REQUIRED);
18
+ return result;
19
+ }
20
+
21
+ if (result.constraint.length > 0) {
22
+ return result;
23
+ }
24
+
25
+ return true;
26
+ };
@@ -0,0 +1,96 @@
1
+ import { FormField } from "@t/FormField";
2
+ import { ValidResult } from "@t/ValidResult";
3
+ import { RULES } from "src/constants";
4
+ import { validator } from "./validator";
5
+ import * as utils from "src/util/utils";
6
+
7
+ /**
8
+ * 숫자 유효성 체크
9
+ *
10
+ * @param {string} value
11
+ * @param {FormField} field
12
+ * @returns {(ValidResult | boolean)}
13
+ */
14
+ export const numberValidator = (value: string, field: FormField): ValidResult | boolean => {
15
+ if (!field.$instance.isEnableView()) {
16
+ return true;
17
+ }
18
+
19
+ const result: ValidResult = { name: field.name, constraint: [] };
20
+ const numValue = Number(value);
21
+
22
+ if (field.required && utils.isBlank(value)) {
23
+ result.constraint.push(RULES.REQUIRED);
24
+ return result;
25
+ }
26
+
27
+ if (!utils.isNumber(value)) {
28
+ result.constraint.push(RULES.NAN);
29
+ return result;
30
+ }
31
+
32
+ if (validator(value, field, result) !== true) {
33
+ return result;
34
+ }
35
+
36
+ const rule = field.rule;
37
+ if (rule) {
38
+ const isMinimum = utils.isNumber(rule.minimum),
39
+ isMaximum = utils.isNumber(rule.maximum);
40
+
41
+ let minRule = false,
42
+ minExclusive = false,
43
+ maxRule = false,
44
+ maxExclusive = false;
45
+
46
+ if (isMinimum) {
47
+ if (rule.exclusiveMinimum && numValue <= rule.minimum) {
48
+ minExclusive = true;
49
+ } else if (numValue < rule.minimum) {
50
+ minRule = true;
51
+ }
52
+ }
53
+
54
+ if (isMaximum) {
55
+ if (rule.exclusiveMaximum && numValue >= rule.maximum) {
56
+ maxExclusive = true;
57
+ } else if (numValue > rule.maximum) {
58
+ maxRule = true;
59
+ }
60
+ }
61
+
62
+ if (isMinimum && isMaximum && (minRule || minExclusive || maxRule || maxExclusive)) {
63
+ if (rule.exclusiveMinimum && rule.exclusiveMaximum && (minExclusive || maxExclusive)) {
64
+ result.constraint.push(RULES.BETWEEN_EXCLUSIVE_MINMAX);
65
+ } else if (minExclusive) {
66
+ result.constraint.push(RULES.BETWEEN_EXCLUSIVE_MIN);
67
+ } else if (maxExclusive) {
68
+ result.constraint.push(RULES.BETWEEN_EXCLUSIVE_MAX);
69
+ } else {
70
+ result.constraint.push(RULES.BETWEEN);
71
+ }
72
+ } else {
73
+ if (minExclusive) {
74
+ result.constraint.push(RULES.EXCLUSIVE_MIN);
75
+ }
76
+
77
+ if (maxExclusive) {
78
+ result.constraint.push(RULES.EXCLUSIVE_MAX);
79
+ }
80
+
81
+ if (minRule) {
82
+ result.constraint.push(RULES.MIN);
83
+ }
84
+
85
+ if (maxRule) {
86
+ result.constraint.push(RULES.MAX);
87
+ }
88
+ }
89
+ }
90
+
91
+ if (result.constraint.length > 0) {
92
+ return result;
93
+ }
94
+
95
+ return true;
96
+ };
@@ -0,0 +1,45 @@
1
+ import { FormField } from "@t/FormField";
2
+ import { ValidResult } from "@t/ValidResult";
3
+
4
+ const regexp = {
5
+ mobile: /^\d{3}-\d{3,4}-\d{4}$/,
6
+ email: /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$/,
7
+ url: /^(?:(?:https?|HTTPS?|ftp|FTP):\/\/)(?:\S+(?::\S*)?@)?(?:(?!(?:10|127)(?:\.\d{1,3}){3})(?!(?:169\.254|192\.168)(?:\.\d{1,3}){2})(?!172\.(?:1[6-9]|2\d|3[0-1])(?:\.\d{1,3}){2})(?:[1-9]\d?|1\d\d|2[01]\d|22[0-3])(?:\.(?:1?\d{1,2}|2[0-4]\d|25[0-5])){2}(?:\.(?:[1-9]\d?|1\d\d|2[0-4]\d|25[0-4]))|(?:(?:[a-zA-Z\u00a1-\uffff0-9]-*)*[a-zA-Z\u00a1-\uffff0-9]+)(?:\.(?:[a-zA-Z\u00a1-\uffff0-9]-*)*[a-zA-Z\u00a1-\uffff0-9]+)*(?:\.(?:[a-zA-Z\u00a1-\uffff]{2,}))\.?)(?::\d{2,5})?(?:[/?#]\S*)?$/,
8
+ number: /\d$/,
9
+ alpha: /^[a-zA-Z]+$/,
10
+ "alpha-num": /^[a-zA-Z0-9]+$/,
11
+ variable: /^[a-zA-Z0-9_$][a-zA-Z0-9_$]*$/,
12
+ "number-char": /^(?=.*[A-Za-z])(?=.*\d)[A-Za-z\d]/,
13
+ "upper-char": /^(?=.*?[A-Z])(?=.*?[a-z])/,
14
+ "upper-char-special": /^(?=.*?[A-Z])(?=.*?[a-z])(?=.*?[#?!@$%^&*-])/,
15
+ "upper-char-special-number": /^(?=.*[A-Za-z])(?=.*\d)(?=.*[@$!%*#?&])[A-Za-z\d@$!%*#?&]/,
16
+ };
17
+
18
+ /**
19
+ * 정규식 유효성 체크.
20
+ *
21
+ * @param {string} value
22
+ * @param {FormField} field
23
+ * @param {(ValidResult | undefined)} result
24
+ * @returns {ValidResult}
25
+ */
26
+ export const regexpValidator = (value: string, field: FormField, result: ValidResult): ValidResult => {
27
+ if (!field.$instance.isEnableView()) {
28
+ return { name: field.name, constraint: [] };
29
+ }
30
+
31
+ if (typeof result === "undefined") {
32
+ result = { name: field.name, constraint: [] };
33
+ }
34
+
35
+ const regexpType = field.regexpType;
36
+
37
+ if (regexpType) {
38
+ if (!regexp[regexpType].test(value)) {
39
+ result.regexp = regexpType;
40
+ return result;
41
+ }
42
+ }
43
+
44
+ return result;
45
+ };
@@ -0,0 +1,64 @@
1
+ import { FormField } from "@t/FormField";
2
+ import { ValidResult } from "@t/ValidResult";
3
+ import { RULES } from "src/constants";
4
+ import * as utils from "src/util/utils";
5
+ import { validator } from "./validator";
6
+
7
+ /**
8
+ * string validator
9
+ *
10
+ * @param {string} value
11
+ * @param {FormField} field
12
+ * @returns {(ValidResult | boolean)}
13
+ */
14
+ export const stringValidator = (value: string, field: FormField): ValidResult | boolean => {
15
+ if (!field.$instance.isEnableView()) {
16
+ return true;
17
+ }
18
+
19
+ let result: ValidResult = { name: field.name, constraint: [] };
20
+
21
+ if (field.required && utils.isBlank(value)) {
22
+ result.constraint.push(RULES.REQUIRED);
23
+ return result;
24
+ }
25
+ const validResult = validator(value, field, result);
26
+ if (validResult !== true) {
27
+ return validResult;
28
+ }
29
+
30
+ const rule = field.rule;
31
+
32
+ if (rule) {
33
+ const valueLength = value.length;
34
+
35
+ const isMinNumber = utils.isNumber(rule.minLength),
36
+ isMaxNumber = utils.isNumber(rule.maxLength);
37
+
38
+ let minRule = false,
39
+ maxRule = false;
40
+ if (isMinNumber && valueLength < rule.minLength) {
41
+ minRule = true;
42
+ }
43
+ if (isMaxNumber && valueLength > rule.maxLength) {
44
+ maxRule = true;
45
+ }
46
+
47
+ if (isMinNumber && isMaxNumber && (minRule || maxRule)) {
48
+ result.constraint.push(RULES.BETWEEN);
49
+ } else {
50
+ if (minRule) {
51
+ result.constraint.push(RULES.MIN_LENGTH);
52
+ }
53
+
54
+ if (maxRule) {
55
+ result.constraint.push(RULES.MAX_LENGTH);
56
+ }
57
+ }
58
+ if (result.constraint.length > 0) {
59
+ return result;
60
+ }
61
+ }
62
+
63
+ return true;
64
+ };
@@ -0,0 +1,49 @@
1
+ import { FormField } from "@t/FormField";
2
+ import { ValidResult } from "@t/ValidResult";
3
+ import { regexpValidator } from "./regexpValidator";
4
+ import * as utils from "src/util/utils";
5
+
6
+ /**
7
+ * validator , regexp 체크 .
8
+ *
9
+ * @param {string} value field value
10
+ * @param {FormField} field field 정보
11
+ * @param {ValidResult} result
12
+ * @returns {(ValidResult | boolean)}
13
+ */
14
+ export const validator = (value: string, field: FormField, result: ValidResult): ValidResult | boolean => {
15
+ if (field.validator) {
16
+ result.validator = field.validator(field, value);
17
+ if (typeof result.validator === "object") {
18
+ return result;
19
+ }
20
+ }
21
+
22
+ result = regexpValidator(value, field, result);
23
+
24
+ if (result.regexp) {
25
+ return result;
26
+ }
27
+
28
+ if (field.different) {
29
+ const diffFieldName = field.different.field;
30
+ const diffField = field.$instance.getForm().getField(diffFieldName);
31
+
32
+ if (!utils.isEmpty(diffField) && field.$instance.getValue() == diffField.$instance.getValue()) {
33
+ result.message = field.different.message;
34
+ return result;
35
+ }
36
+ }
37
+
38
+ if (field.identical) {
39
+ const diffFieldName = field.identical.field;
40
+ const diffField = field.$instance.getForm().getField(diffFieldName);
41
+
42
+ if (!utils.isEmpty(diffField) && field.$instance.getValue() != diffField.$instance.getValue()) {
43
+ result.message = field.identical.message;
44
+ return result;
45
+ }
46
+ }
47
+
48
+ return true;
49
+ };
@@ -0,0 +1,17 @@
1
+ /**
2
+ *
3
+ * @interface StringKeyMap
4
+ * @typedef {StringKeyMap}
5
+ */
6
+ export interface StringKeyMap {
7
+ [key: string]: any;
8
+ }
9
+
10
+ /**
11
+ *
12
+ * @interface NumberKeyMap
13
+ * @typedef {NumberKeyMap}
14
+ */
15
+ export interface NumberKeyMap {
16
+ [key: number]: any;
17
+ }