summaryrefslogtreecommitdiffstats
path: root/catalog-ui/src/app/directives/utils/validation-on-load/validation-on-load.ts
blob: d489efa928b89d6bbece55cc52134f8a26d638f8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
'use strict';

export interface IValidationOnLoadScope extends ng.IScope {
    formToValidate:ng.IFormController;
}

export class ValidationOnLoadDirective implements ng.IDirective {

    constructor(private $timeout:ng.ITimeoutService) {
    }

    scope = {
        formToValidate: '='
    };

    public replace = false;
    public restrict = 'A';


    public link = (scope:IValidationOnLoadScope, $elem:ng.IAugmentedJQuery, $attrs:angular.IAttributes) => {

        let init = ()=> {
            //validate errors
            if (scope.formToValidate.$error) {
                angular.forEach(scope.formToValidate.$error, (value, key)=> {
                    //skip on the required error if its a new form
                    if (key != 'required') {
                        angular.forEach(value, function (field) {
                            field.$setDirty();//trigger to show the error label
                        });
                    }
                })
            }
        };

        this.$timeout(()=> {
            init();
        }, 0);

    };

    public static factory = ($timeout:ng.ITimeoutService)=> {
        return new ValidationOnLoadDirective($timeout);
    };

}

ValidationOnLoadDirective.factory.$inject = ['$timeout'];