tildeio / htmlbars

A variant of Handlebars that emits DOM and allows you to write helpers that manipulate live DOM nodes
MIT License
1.6k stars 193 forks source link

Input type='date' #472

Open fsmanuel opened 8 years ago

fsmanuel commented 8 years ago

Initially reported: https://github.com/martndemus/ember-form-for/issues/123 (with a demo)

Browsers adjust the date to the local format. That results in the following error if you try to enter 27.11.2016 (valid german format):

The specified value "2-11-27" does not conform to the required format, "yyyy-MM-dd".

jQuery has the same problem but can be tweaked with valHooks.date. See https://github.com/martndemus/ember-form-for/issues/123#issuecomment-263118713 how the hook can be implemented.

Still htmlbars setPropertyStrict throws an error and the date is reset because of the wrong format.

A fix could be:

prototype.setPropertyStrict = function (element, name, value) {
  if (value === undefined) {
    value = null;
  }

  if (value === null && (name === 'value' || name === 'type' || name === 'src')) {
    value = '';
  }

  // Prevents the date to be reseted if the format isn't yyyy-mm-dd
  if (element.type === 'date') {
    let [year] = value.split('-');

    if (year.length < 4) {
      return;
    }
  }

  element[name] = value;
};

I'm happy to discuss a better fix or solution to the problem.