Skip to content
This repository was archived by the owner on Jun 27, 2023. It is now read-only.

Commit cedf3d2

Browse files
committed
fix(validation): changed min, max length params and FieldControl factory
1 parent f3dc2dd commit cedf3d2

File tree

3 files changed

+20
-46
lines changed

3 files changed

+20
-46
lines changed

src/components/dynamic-form/DynamicForm.vue

Lines changed: 15 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ import {
6161
} from '@/core/models';
6262
import { dynamicFormsSymbol } from '@/useApi';
6363
import { deepClone, hasValue, removeEmpty } from '@/core/utils/helpers';
64+
import { FieldControl } from '@/core/factories';
6465
6566
const props = {
6667
form: {
@@ -73,12 +74,6 @@ const components = {
7374
DynamicInput,
7475
};
7576
76-
const EMPTY_CONTROL = {
77-
dirty: false,
78-
touched: false,
79-
valid: true,
80-
};
81-
8277
/* const AVAILABLE_THEMES = ['default', 'material'];
8378
*/
8479
export default defineComponent({
@@ -168,17 +163,15 @@ export default defineComponent({
168163
Object.entries(props.form?.fields).map(
169164
([key, field]: [string, InputType]) =>
170165
empty
171-
? ({
166+
? FieldControl({
172167
...field,
173168
name: key,
174169
value: null,
175-
...EMPTY_CONTROL,
176-
} as FormControl<InputType>)
177-
: ({
170+
})
171+
: FieldControl({
178172
...field,
179173
name: key,
180-
...EMPTY_CONTROL,
181-
} as FormControl<InputType>),
174+
}),
182175
) || [];
183176
if (props.form.fieldOrder) {
184177
controls.value = controlArray.sort(
@@ -225,41 +218,22 @@ export default defineComponent({
225218
}
226219
}
227220
228-
/* function validateControl(control: FormControl<InputType>) {
229-
if (control.validations) {
230-
const validation = control.validations.reduce((prev, curr) => {
231-
const val =
232-
typeof curr.validator === 'function'
233-
? curr.validator(control)
234-
: null;
235-
if (val !== null) {
236-
const [key, value] = Object.entries(val)[0];
237-
const obj = {};
238-
obj[key] = {
239-
value,
240-
text: curr.text,
241-
};
242-
return {
243-
...prev,
244-
...obj,
245-
};
246-
}
247-
return {
248-
...prev,
249-
};
250-
}, {});
251-
control.errors = validation;
252-
control.valid = Object.keys(validation).length === 0;
253-
}
254-
} */
255-
256221
function detectChanges(fields) {
257222
const changes = diff(cache, deepClone(fields));
258223
Object.entries(changes).forEach(([key, value]) => {
259224
let ctrl = findControlByName(key);
260225
if (ctrl) {
261226
Object.entries(value).forEach(([change, newValue]) => {
262-
ctrl[change] = newValue;
227+
if (change === 'options' || change === 'validations') {
228+
Object.entries(newValue).forEach(([optKey, optValue]) => {
229+
ctrl[change][optKey] = {
230+
...ctrl[change][optKey],
231+
...optValue,
232+
};
233+
});
234+
} else {
235+
ctrl[change] = newValue;
236+
}
263237
});
264238
}
265239
});

src/core/factories.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ export const FieldControl = ({
149149
type,
150150
...rest
151151
}: Partial<FormControl<any>>): FormControl<any> => ({
152-
...FieldBase(rest),
152+
...rest,
153153
name,
154154
type,
155155
...EMPTY_CONTROL,

src/core/utils/validators.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ export const url = (value: string): ValidationErrors => {
4747
};
4848

4949
export const minLength = (minLength: number) => (
50-
value: number,
50+
value: string,
5151
): ValidationErrors => {
5252
if (isEmptyInputValue(value)) {
5353
return { minLength: null }; // don't validate empty values to allow optional controls
@@ -63,14 +63,14 @@ export const minLength = (minLength: number) => (
6363
};
6464

6565
export const maxLength = (maxLength: number) => (
66-
value: number,
66+
value: string,
6767
): ValidationErrors => {
6868
if (isEmptyInputValue(value)) {
69-
return null; // don't validate empty values to allow optional controls
69+
return { maxLength: null }; // don't validate empty values to allow optional controls
7070
}
7171
const length = value ? `${value}`.length : 0;
7272
return {
73-
maxlength:
73+
maxLength:
7474
length > maxLength
7575
? { requiredLength: maxLength, actualLength: length }
7676
: null,

0 commit comments

Comments
 (0)