$setDirty function should provided by the controller of ng-model directive #10038
Description
I wan to show the error message when the user submit an invalid and pristine form.
I use the following css selector to style the message, since that I don't wan to show message if the form is pristine.
.ng-dirty.ng-invalid {
border-color:red;
xxx
}
So I do some check in the form submit callbak (ngSubmit
), if the form is invalid, I wan to manually set the error control to dirty state, so that the css selector which is .ng-dirty.ng-invalid
work.
<form name="theForm" ng-submit="doSubmit()">xxx</form>
$scope.doSubmit = function(){
if($scope.theForm.$invalid){
Object.keys($scope.theForm.$error).forEach(function (key) {
$scope.theForm.$error[key].forEach(function (control) {
control.$setDirty(); // no $setDirty function
});
});
return;
}
// ajax submit
// ...
}
There is $setDiry
function in FormController
, but I can't use it, because the function only add ng-dirty
class to form element, if I use selector .ng-dirty .ng-invalid
insteaded, it will apply to too much input element that all the error message come out if any input change.
I found the $setPristine
function in input directive, I think it should provide $setDirty
too.