I use an angular directive to change the form input validity as soon as the user starts to fill the input :
angular.module('core').directive('serverError', function() {
return {
restrict: 'A',
require: '?ngModel',
link: function(scope, element, attrs, ctrl) {
element.on('input', function() {
scope.$apply(function() {
ctrl.$setValidity('server', true);
});
});
element.on('change', function() {
scope.$apply(function() {
ctrl.$setValidity('server', true);
});
});
}
};
});
My form is :
<input type="text" data-ng-model="request.lastname" name="lastname" required server-error/>
<div ng-show="form.lastname.$error.server">
<span>Please fill this field</span>
</div>
<input type="radio" data-ng-model="request.civility" name="civility" value="M" required server-error> M.
<input type="radio" data-ng-model="request.civility" name="civility" value="F" required server-error> Mme
<input type="radio" data-ng-model="request.civility" name="civility" value="MISS" required server-error> Mlle
<div ng-show="form.civility.$error.server">
<span>Please fill this field</span>
</div>
It works correctly for the text inputs but it doesn't work for the radio buttons. Only the last radio button works as expected and make the error disappears.
Any idea to resolve this problem?
Thanks.