I want to set a field of a particular form as dirty as I am manually changing that value.I have already gone the the SO thread Angular.js programmatically setting a form field to dirty , But no luck.
Here is the sample replica of my issue. Plunk
<html ng-app="sampleApp">
<head>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.0- rc.4/angular.min.js"></script>
<style type="text/css">
input.ng-invalid {
border: 1px solid red;
}
input.ng-valid {
border: 1px solid green;
}
input.ng-pristine {
border-color: #FFFF00;
}
</style>
<script type="text/javascript">
var sampleApp = angular.module("sampleApp", []);
sampleApp.controller('sampleCtrl', ['$scope', '$http', '$timeout',
function($scope, $http, $timeout) {
$scope.userData = {
username: "",
};
$timeout(function() {
$scope.userData.username = '$#deepak';
}, 5000);
}
]);
sampleApp.directive('myUsername', [
function() {
// Runs during compile
return {
require: 'ngModel', // Array = multiple requires, ? = optional, ^ = check parent
link: function($scope, iElm, iAttrs, controller) {
controller.$parsers.unshift(function(value) {
//allow only alphanumeric characters
var reg = /^\w+$/;
var isValidUsername = reg.test(value);
controller.$setValidity('username', isValidUsername);
return isValidUsername ? value : undefined;
});
controller.$formatters.unshift(function(value) {
var reg = /^\w+$/;
var isValidUsername = reg.test(value);
controller.$setValidity('username', isValidUsername);
$scop e.registrationForm.username.$setViewValue($scope.registrationForm.username.$viewValue);
return value;
})
}
};
}
]);
</script>
</head>
<body ng-controller="sampleCtrl">
<div ng-show="bShowStatus">
{{message}}
<ul ng-repeat="err in errors">
{{err}}
</ul>
</div>
<form name="registrationForm" ng-submit="submitForm()" novalidate>
UserName
<br>
<input type="text" name="username" ng-model="userData.username" ng-model-options="{updateOn: 'blur'}" my-username>
<span ng-show="registrationForm.username.$dirty && registrationForm.username.$error.username">
Invalid Username.
</span>
<br>
<br>
<input type="submit" value="Submit">
</form>
</body>
</html>
In the sample I am modifying the username after 5 second (only for the example). At the same time , I want to set the field as dirty and fire the validation and show the error message.
Please help.