angular-datepicker icon indicating copy to clipboard operation
angular-datepicker copied to clipboard

text input not valid when min-date and/or max-date is set

Open RobYed opened this issue 8 years ago • 2 comments

I am using Angular v1.4.9 and angular-datepicker from commit 41d949bf91f9049a8407c89be2c6d11e8a485ad3. I have two identical date-time inputs, except that one uses min-date and max-date and the other one does not. Interestingly enough a ng-change function I registered is called on the input without min and max, but is not called on the other one. I also tested date-change, which unfortunately only works on datepicker changes, not text inputs.

The one with min and max looks like this:

<input type="datetime"
       id="datetime"
       name="birthday"
       date-time
       ng-model="vm.form.birthday"
       ng-change="vm.stringDateToMoment(user.birthday.$viewValue)"
       min-date="vm.minDate"
       max-date="vm.maxDate"
       ng-blur="vm.checkFormValidity(user.$valid)"
       view="year"
       min-view="date"
       format="DD.MM.YYYY"
       auto-close="true"
       ng-required="true"
       required>

My inital problem was, that plain text input is not converted to a moment instance and produces user.birthday.$error = {"min":true,"max":true}. So, I do the conversion manually with ng-change="vm.stringDateToMoment(user.birthday.$viewValue)". This works fine without min-date and max-date. As soon as I include both, the datepicker works as expected and forbidds dates outside of min and max. But unfortunately ng-change is not called anymore, thus text input is not valid.

For me this is not an expected behaviour as I do not see the relation between those directives.

So, is this a bug or can anyone help me with a workaround?

RobYed avatar Feb 17 '16 15:02 RobYed

I still don't know whether this should be categorized as a Bug. What I found out, was that as soon as I add min-date and/or max-date, manually entered Dates will be validated as false, because they are not a moment instance, but a date string.

I fixed it locally by adding value = moment(value, format, true); to line 780 and 791.

Is this an appropriate fix in your eyes or do you know a better approach?

RobYed avatar Feb 22 '16 13:02 RobYed

+1

wonsikin avatar Sep 27 '17 06:09 wonsikin