ui-date
ui-date copied to clipboard
Initial state of a ui-date control is dirty (ng-dirty) causing the form state to be dirty as well
When you add ui-date directive to an input control the initial state of the control is dirty causing the form to be dirty as well. The control should only be dirty when the user changes the value.
https://plnkr.co/edit/QaM7f8uOn4mm11JiYAwr
I have tracked it down to the following code
if (controller) {
controller.$render();
// Update the model with the value from the datepicker after parsed
setVal(true); // *** this causes control to become dirty ***
}
Thanks for tracking that down, I will not have much time to update this so PRs are welcome.
@alexanderchan I understand and will see if I can get to it next week
+1
If I add controller.$setPristine();
after setVal(true);
it seems to work.
Too simple? `
can you guys fix it ?
You can just add after lines:
if (controller) {
controller.$render();
// Update the model with the value from the datepicker after parsed
setVal(true);
controller.$setPristine();
additional code to set form $dirty field to false
if (controller.$$parentForm) {
var anyOther = false;
for (var key in controller.$$parentForm) {
if (key.indexOf('$') != 0) {
var siblingController = controller.$$parentForm[key];
if (siblingController.$dirty) {
anyOther = true;
break;
}
}
}
if (!anyOther) {
controller.$$parentForm.$setPristine()
}
}