angularjs - Event being triggered even though form field is $pristine -
my form has 2 input fields below:
<input type="password" name="password" ng-model="password" placeholder="enter password" class="form-control" /> <input type="password" name="password_confirm" ng-model="password_confirm" placeholder="confirm password" class="form-control" validate-equals="password" />
please match passwords expected: error below second input should trigger if second input field $dirty , not matching first field
however behavior not achieved.
app.directive('validateequals', function(){ return { require: "ngmodel", link: function(scope, element, attrs, ngmodelctrl) { function validateequal(value) { var valid = (value === scope.$eval(attrs.validateequals)); ngmodelctrl.$setvalidity('equal', valid); return valid ? value : undefined; } ngmodelctrl.$parsers.push(validateequal); ngmodelctrl.$formatters.push(validateequal); scope.$watch(attrs.validateequals, function(){ ngmodelctrl.$setviewvalue(ngmodelctrl.$viewvalue) }); } }; })
please check full plnkr here
the culprit watch:
scope.$watch(attrs.validateequals, function(){ ngmodelctrl.$setviewvalue(ngmodelctrl.$viewvalue) });
at startup $watch
fires, calling $setviewvalue()
on input you've set attribute validateequals
on (specifically, password_confirm
). calling $setviewvalue
results in field being marked $dirty
(since you're setting field something).
i'm not seeing reason $watch
, sets viewvalue
whatever was. unless i'm missing something, should fine removing it- solving problem.
working plnkr: http://plnkr.co/edit/4qajucklva2mwajacgcf?p=preview
Comments
Post a Comment