• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

atinc / ngx-tethys / d9ae709b-3c27-4b69-b125-b8b80b54f90b

pending completion
d9ae709b-3c27-4b69-b125-b8b80b54f90b

Pull #2757

circleci

mengshuicmq
fix: fix code review
Pull Request #2757: feat(color-picker): color-picker support disabled (#INFR-8645)

98 of 6315 branches covered (1.55%)

Branch coverage included in aggregate %.

1 of 1 new or added line in 1 file covered. (100.0%)

2392 of 13661 relevant lines covered (17.51%)

83.12 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

9.41
/src/form/form-validator-loader.ts
1
import { Dictionary } from 'ngx-tethys/types';
2
import { helpers } from 'ngx-tethys/util';
3

4
import { Inject, Injectable, Optional } from '@angular/core';
5
import { ValidationErrors } from '@angular/forms';
1✔
6

1✔
7
import { THY_VALIDATOR_CONFIG, ThyFormValidationMessages, ThyFormValidatorGlobalConfig } from './form.class';
1✔
8

1✔
9
export const ERROR_VALUE_REPLACE_REGEX = /\{(.+?)\}/g;
10

11
const INVALID_CLASS = 'is-invalid';
12
const INVALID_FEEDBACK_CLASS = 'invalid-feedback';
13

1✔
14
const defaultValidatorConfig: ThyFormValidatorGlobalConfig = {
15
    showElementError: true,
16
    removeElementError: true,
17
    validationMessages: {}
18
};
19

20
const globalValidationMessages = {
21
    required: '该选项不能为空',
22
    maxlength: '该选项输入值长度不能大于{maxlength}',
23
    minlength: '该选项输入值长度不能小于{minlength}',
24
    thyUniqueCheck: '输入值已经存在,请重新输入',
25
    email: '输入邮件的格式不正确',
26
    confirm: '两次输入不一致',
27
    pattern: '该选项输入格式不正确',
28
    number: '必须输入数字',
29
    url: '输入URL格式不正确',
1✔
30
    max: '该选项输入值不能大于{max}',
31
    min: '该选项输入值不能小于{min}'
×
32
};
×
33

34
/**
35
 * @private
×
36
 */
37
@Injectable()
38
export class ThyFormValidatorLoader {
39
    private config: ThyFormValidatorGlobalConfig;
×
40

41
    private getDefaultValidationMessage(key: string) {
42
        if (this.config.globalValidationMessages && this.config.globalValidationMessages[key]) {
×
43
            return this.config.globalValidationMessages[key];
44
        } else {
45
            return globalValidationMessages[key];
×
46
        }
×
47
    }
48
    constructor(
×
49
        @Optional()
50
        @Inject(THY_VALIDATOR_CONFIG)
51
        config: ThyFormValidatorGlobalConfig
×
52
    ) {
53
        this.config = Object.assign({}, defaultValidatorConfig, config);
54
    }
×
55

×
56
    get validationMessages() {
57
        return this.config.validationMessages;
58
    }
×
59

60
    get validateOn() {
61
        if (!this.config?.validateOn) {
62
            this.config.validateOn = 'submit';
×
63
        }
×
64
        return this.config.validateOn;
×
65
    }
×
66

67
    isElementInInputGroup(element: HTMLElement) {
68
        return !!(element?.parentElement?.tagName.toUpperCase() === 'THY-INPUT-GROUP');
×
69
    }
70

71
    getErrorMessage(name: string, key: string): string {
×
72
        if (this.validationMessages[name] && this.validationMessages[name][key]) {
×
73
            return this.validationMessages[name][key];
×
74
        } else {
×
75
            return this.getDefaultValidationMessage(key);
×
76
        }
×
77
    }
×
78

×
79
    getErrorMessages(name: string, validationErrors: ValidationErrors): string[] {
80
        const messages = [];
81
        for (const validationError in validationErrors) {
82
            if (validationErrors.hasOwnProperty(validationError)) {
×
83
                messages.push(this.getErrorMessage(name, validationError));
×
84
            }
×
85
        }
×
86
        return messages;
87
    }
88

89
    defaultShowError(element: HTMLElement, errorMessages: string[]) {
90
        if (element && element.parentElement) {
×
91
            const documentFrag = document.createDocumentFragment();
×
92
            const divNode = document.createElement('DIV');
×
93
            const textNode = document.createTextNode(errorMessages[0]);
×
94
            divNode.appendChild(textNode);
95
            divNode.setAttribute('class', INVALID_FEEDBACK_CLASS);
×
96
            documentFrag.appendChild(divNode);
×
97
            element.parentElement.append(documentFrag);
98
        }
99
    }
100

101
    defaultRemoveError(element: HTMLElement) {
102
        if (element && element.parentElement) {
103
            const invalidFeedback = element.parentElement.querySelector('.invalid-feedback');
×
104
            if (invalidFeedback) {
×
105
                element.parentElement.removeChild(invalidFeedback);
×
106
            }
×
107
        }
108
    }
×
109

×
110
    removeError(element: HTMLElement) {
111
        const formControlElement = this.isElementInInputGroup(element) ? element.parentElement : element;
112
        formControlElement.classList.remove(INVALID_CLASS);
113
        if (helpers.isFunction(this.config.removeElementError)) {
114
            this.config.removeElementError(formControlElement);
115
        } else if (this.config.showElementError) {
116
            this.defaultRemoveError(formControlElement);
×
117
        } else {
118
            // do nothings
119
        }
×
120
    }
121

1✔
122
    showError(element: HTMLElement, errorMessages: string[]) {
123
        const formControlElement = this.isElementInInputGroup(element) ? element.parentElement : element;
124
        formControlElement.classList.add(INVALID_CLASS);
125
        if (helpers.isFunction(this.config.showElementError)) {
1✔
126
            this.config.showElementError(formControlElement, errorMessages);
127
        } else if (this.config.showElementError) {
128
            this.defaultShowError(formControlElement, errorMessages);
129
        } else {
130
            // do nothings
131
        }
132
    }
133

134
    addValidationMessages(messages: ThyFormValidationMessages) {
135
        Object.assign(this.config.validationMessages, messages);
136
    }
137

138
    setGlobalValidationMessages(validationMessages: Dictionary<string>) {
139
        this.config.globalValidationMessages = validationMessages;
140
    }
141
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc