Whenever a user opens a modal window to save data, I reset the form fields to blank. This works as expected, but I encountered an issue with AngularJS form validation messages appearing due to dirty check. I tried adding $setPristine()
to resolve this, but it resulted in an error stating that $setPristine()
is undefined.
main.html
<form id="editMessagesForm" name="editMessagesForm"
novalidate class="border-box-sizing">
<div class="modalForm">
<div class="modalBorder">
<div class="row" ng-if="messageNotificationDTO.adminNotificationKey">
<div class="form-group col-md-12 fieldHeight">
<label class="col-md-4">Message Key:</label>
<div class="col-md-8">
<input type="text" class="form-control" id="messageNotificationKey"
name="messageNotificationKey"
ng-model="messageNotificationDTO.adminNotificationKey"
disabled="disabled">
</div>
</div>
</div>
<div class="row">
<div class="form-group col-md-12 fieldHeight">
<label class="col-md-4">Message Type:</label>
<div class="col-md-8">
<select name="mesgLookUpcode" class="form-control"
ng-model="messageNotificationDTO.adminNotificationTypeCode" required
id="mesgLookUpcode"
ng-options="adminDataSource.id as adminDataSource.text for adminDataSource in adminDataSource">
<option value="">Select...</option>
</select>
</div>
</div>
</div>
<div class="row">
<div class="form-group col-md-12 fieldHeight">
<label class="col-md-4 required">Notification Name:</label>
<div class="col-md-8">
<textarea rows="2" class="form-control"
ng-model="messageNotificationDTO.adminNotificationName"
name="adminNotificationName"
required
id="adminNotificationName" txt-area-maxlen="128"
ng-disabled="readOnlyFlag"
data-tooltip-html-unsafe="<div>{{128 - messageNotificationDTO.adminNotificationName.length}} characters left</div>"
tooltip-trigger="{{{true: 'focus', false: 'never'}[messageNotificationDTO.adminNotificationName.length >= 0 || messageNotificationDTO.adminNotificationName.length == null ]}}"
tooltip-placement="top" tooltip-class = "bluefill"></textarea>
<p class="text-danger" ng-show="editMessagesForm.adminNotificationName.$touched && editMessagesForm.adminNotificationName.$error.required">Message Notification Name is required</p>
</div>
</div>
</div>
</div>
</div>
<div class="modal-footer footerMargin">
<button type="button" class="btn btn-primary pull-right mousedwncall"
ng-click="saveMessages()" ng-disabled="editMessagesForm.$invalid"
ng-class="{disableSaveCls:editMessagesForm.$invalid}"
>Save</button>
<button class="btn btn-default pull-left mousedwncall"
ng-click="handleCancelMessageModal()">Cancel</button>
</div>
</form>
main.js
$scope.addMessageNotification = function() {
clearNotificationForm();
$scope.editMessagesForm.$setPristine();
$scope.messageNotificationModal.open().center();
};
var clearNotificationForm = function () {
$scope.messageNotificationDTO.adminNotificationTypeCode = [];
$scope.messageNotificationDTO.adminNotificationName = '';
$scope.messageNotificationDTO.adminNotificationBody = '';
$scope.messageNotificationDTO.adminNotificationStartTime = '';
$scope.messageNotificationDTO.adminNotificationEndTime = '';
$scope.messageNotificationDTO.activeFlag = '';
};
ctrl.js
$scope.messageNotificationDTO = {
adminNotificationTypeCode: [],
adminNotificationName:'',
adminNotificationBody: '',
adminNotificationStartTime: '',
adminNotificationEndTime: '',
activeFlag: ''
};
$scope.adminDataSource = adminData.data;
//Set notification grid config and dataSource
$scope.messageGridOptions = messageGridConfig.messagesGridOptions;
messageGridConfig.messagesGridOptions.dataSource = MessageAdminNotificationFactory.getNotificationDataSource();
//Save Notification
$scope.saveMessages = function() {
MessageAdminNotificationFactory.saveMessageAdmin($scope.messageNotificationDTO).then(function() {
$scope.messageNotificationModal.close();
$scope.messageGridOptions.dataSource.read();
clearNotificationForm();
});
};
//Add new Notification
$scope.addMessageNotification = function() {
$scope.messageNotificationModal.open().center();
};
var clearNotificationForm = function () {
$scope.messageNotificationDTO.adminNotificationTypeCode = [];
$scope.messageNotificationDTO.adminNotificationName = '';
$scope.messageNotificationDTO.adminNotificationBody = '';
$scope.messageNotificationDTO.adminNotificationStartTime = '';
$scope.messageNotificationDTO.adminNotificationEndTime = '';
$scope.messageNotificationDTO.activeFlag = '';
$scope.editMessagesForm.$setPristine();
};
error
Cannot read property '$setPristine' of undefined