ui-date icon indicating copy to clipboard operation
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

Open cs-NET opened this issue 8 years ago • 6 comments

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

cs-NET avatar Mar 07 '16 19:03 cs-NET

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 *** 
          }

cs-NET avatar Mar 07 '16 19:03 cs-NET

Thanks for tracking that down, I will not have much time to update this so PRs are welcome.

alexanderchan avatar Mar 10 '16 23:03 alexanderchan

@alexanderchan I understand and will see if I can get to it next week

cs-NET avatar Mar 11 '16 00:03 cs-NET

+1

If I add controller.$setPristine(); after setVal(true); it seems to work.

Too simple? `

deggesim avatar Jun 17 '16 13:06 deggesim

can you guys fix it ?

zzsoszz avatar Aug 25 '16 05:08 zzsoszz

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()
		        }
		      }

rbarinov avatar Dec 10 '16 12:12 rbarinov