formly-js / angular-formly

JavaScript powered forms for AngularJS
http://docs.angular-formly.com
MIT License
2.22k stars 404 forks source link

ui-datepicker example, shows $error when initial value is unchanged #551

Closed blowsie closed 8 years ago

blowsie commented 8 years ago

I updated the the ui-datepicker example to have an initial value in the model

vm.model = {
       date: "2016-11-03T00:00:00.000Z"
};

http://jsbin.com/gujowivini/1/edit?js,output

This made the field and form have a $error with date.

  "$error": {
      "date": true
  }

Changing the date to another day, and then changing it back again results in the field being valid even though the value is exactly the same.

Any ideas what is going on here?

Thanks

blowsie commented 8 years ago

The issue here is that the string is not a type of date, To fix this I implemented a helper inside the link function to create a date from the model provided.

  link: function(scope){
      var model = scope.model[scope.options.key]
      var isDate = (model instanceof Date)
      if(!isDate){
        scope.model[scope.options.key] = new Date(model);
      }
    },

http://jsbin.com/figofapana/1/edit?js,output