0

I'm working with AngularJs 1.2. I have a problem with $setValidity.

I have a form input with ng-repeat :

<div ng-repeat="(key, value) in addition_page">
    <input ng-model="addition_page[key].name" name="pagename[{{key}}]" not-duplicated="1"> 
</div>

Now I need to use $setValidity on some specify input, but I failed

scope["finish_form"]["pagename[0]"].$setValidity('not-duplicated', false);

I tried to log:

console.log(scope["finish_form"]["pagename[0]"]) => return undefined
console.log(scope["finish_form"]["pagename[{{key}}]"]) => return last input

I inspected element on browser, pagename[0] is existed.

I need help, plz.

Thank you all.

Update:

.directive('notDuplicated', [function() {
            return {
                require: 'ngModel',
                link: function(scope, elem, attr, ngModel) {
                    var val_id = attr.notDuplicated;

                    var setValidity = function(value) {
                        if (value === "") return true;
                        var same_now = [];
                        var same_past = [];
                        angular.element("[not-duplicated=" + val_id + "]").each(function(k, v) {
                            if (angular.element(v).val() === value) {
                                same_now.push(k);
                            }
                            if (ngModel.$modelValue !== "" && angular.element(v).val() === ngModel.$modelValue) {
                                same_past.push(k);
                            }
                        })

                        // mark old input is valid
                        if (same_past.length === 1) {
                            scope["finish_form"]["pagename[" + same_past[0] + "]"].$setValidity('not-duplicated', true);
                        }

                        // mark new inputs is invalid
                        if (same_now.length > 1) {
                            same_now.each(function(k) {
                                scope["finish_form"]["pagename[" + k + "]"].$setValidity('not-duplicated', false);
                            });
                            return false;
                        }
                        return true;

                    }

                    //For DOM -> model validation
                    ngModel.$parsers.unshift(function(value) {
                        var valid = setValidity(value);
                        return valid ? value : undefined;
                    });

                    //For model -> DOM validation
                    ngModel.$formatters.unshift(function(value) {
                        setValidity(value);
                        return value;
                    });
                }
            };
        }]);
Trung
  • 650
  • 1
  • 5
  • 15
  • where are you doing this: "scope["finish_form"]["pagename[0]"].$setValidity('not-duplicated', false);"? Are you doing that inside the link function of a custom validation directive? – Josep Sep 12 '14 at 03:35
  • Are you doing that inside the link function of a custom validation directive? => Yes.Inside ngModel.$parsers.unshift and ngModel.$formatters.unshift – Trung Sep 12 '14 at 03:41
  • mmmmmmm, could you please share the rest of that code? – Josep Sep 12 '14 at 03:43
  • I appended my directive. Take a look at that. Thank you. – Trung Sep 12 '14 at 03:51
  • For ngModelController name interpolation `{{}}` do not work. Look at ng-form directive. – Chandermani Sep 12 '14 at 04:18

1 Answers1

1

This happens because the control's name (the one with which it is registered on its parent form) is retrieved during the ngModelController's instantiation, which according to the docs takes place before the pre-linking phase* (so no interpolation yet).

If you inspect the myForm's properties, you will find out that all the inputs are registered as "pagename[{{key}}]".


Take a look at this answer for a more detailed explanation and a possible solution (using an extra directive).

Community
  • 1
  • 1
gkalpak
  • 47,844
  • 8
  • 105
  • 118
  • @LêTrầnTiếnTrung: Glad to know :) Feel free to also upvote the other answer to let other people know it helped you with your problem. – gkalpak Sep 12 '14 at 06:54