I am trying to display an error message generated on the server to display after returning to an angular async validator.
Here is my validator:
(function () {
var asyncValidatorTest = function ($http, $q) {
return {
restrict: "A",
require: "ngModel",
link: function (scope, elem, attrs, ngModel) {
ngModel.$asyncValidators.asyncTest = function (modelValue, viewValue) {
var userInput = modelValue || viewValue;
var message = attrs[asyncValidatorTest];
return $http({
method: "POST",
url: "/AngularTest/AsyncValidatorTest",
data: { input: userInput }
})
.then(function (response) {
return true;
}, function (response) {
message = response.statusText;
return $q.reject(response.statusText);
});
}
}
}
}
angular.module("angularApp").directive("asyncValidatorTest", ["$http", "$q", asyncValidatorTest]);
}())
Here is the html:
<div class="form-group" ng-class="{'has-error': (testForm.$submitted && testForm.asyncValidateTest.$invalid) || (testForm.asyncValidateTest.$invalid && testForm.asyncValidateTest.$dirty)}">
<label for="validateTest" class="control-label">Async Validate Test</label>
<input type="text" name="asyncValidateTest" class="form-control" ng-model="vt.message" ng-model-options="{updateOn: 'blur'}" async-validator-test="vt.serverError" required />
<span class="help-block" ng-show="(testForm.$submitted && testForm.asyncValidateTest.$error.required) || (testForm.asyncValidateTest.$error.required && testForm.asyncValidateTest.$dirty)">Input value is required.</span>
<div ng-show="!testForm.asyncValidateTest.$error.required">
<div ng-show="(testForm.$submitted && testForm.asyncValidateTest.$invalid) || (testForm.asyncValidateTest.$invalid && testForm.asyncValidateTest.$dirty)">
<span class="help-block">{{ vt.serverError }}</span>
</div>
</div>
</div>
What am I missing? Obviously there is something.