javascript - Serverside validation of AngularJS forms, use $setValidity in the view controller? -
or put in tl;dr code:
form.email.$setvalidity('conflict', false);
is sticky simple serverside validation flow.
i'm trying form show feedback in event user enters email address in use customer. i'm running angularjs v1.2 , have template:
<form name="form"> <input name="email" type="email" ng-model="..." required> </form> <div ng-messages="form.email.$error"> <div ng-message="conflict">email address in use.</div> </div>
in controller, i'll handle submit event , trigger validation in $http.post().error
handler this:
$http.post('api/form/submit/path/here').error(function(resp) { if (resp.details === 'conflict') $scope.form.email.$setvalidity('conflict', false); });
the problem when user goes , changes value in input field, error message doesn't go away. sticks around until manually call $scope.form.setvalidity();
.
the docs implement custom directive ng-model
dependency, seems super overkill purposes. i've tried setting $scope.form.email.$valid = false;
, $scope.form.email.$invalid = true;
don't change appearance of textbox.
nothing in code modifies conflict
validation key, except when $setvalidity('conflict', false)
explicitly called. since code setting state of conflict
validation key , there nothing else resetting true, it's expected behaviour editing textbox wouldn't reset conflict
validation state.
to behaviour want, need code it. 1 way use ng-change
.
<input name="email" type="email" ng-model="..." required ng-change="resetconflictstate()"> $scope.resetconflictstate = function() { $scope.form.email.$setvalidity('conflict', true); }
Comments
Post a Comment