Removing a value from a date type

I’ve got some table cells that are formatted as follows:

{
type: ‘date’,
dateFormat: ‘MM/DD/YYYY’,
correctFormat: true,
}

When I enter a value, it works fine. But, once the cell is non-blank, I can’t seem to delete the value (without the delete key, or backspace), without it staying red in color (being invalid). How can I allow users to delete the content of a date cell?

John

Hi, @fisher23!

If you want to allow the user to delete the date value, but don’t mind it still being highlighted as invalid, use the allowInvalid option. What it does, is allowing to pass invalid values (and an empty value is not a proper date, so it is invalid), but the validator still works as it should, so it marks the cell red.

If you’d like, however to make it possible to delete the date-typed cell’s content without it being marked as invalid, but still use the validation on non-empty cells, you will have to make a simple custom validator.

For example:

var customDateValidator = function(value, callback) {
  if (value === '') {
    callback(true);
  } else {
    Handsontable.DateValidator.call(this, value, callback);
  }
};

and applying it in the Handsontable initial settings in the validator property:

{
  type: 'date',
  dateFormat: 'MM/DD/YYYY',
  validator: customDateValidator,
  allowInvalid: true
},

I made a JSFiddle with this example: http://jsfiddle.net/ok4ar0pv/

Does this answer your question?

Ok, thanks; that seems to work functionally, but its giving me an error with my TypeScript compilation (it doesn’t know what Handsontable.DataValidator is). Is there a newer Typescript file available then this one?

https://github.com/DefinitelyTyped/DefinitelyTyped/tree/master/jquery-handsontable

There is no TS file officially supported yet. Could you paste the exact error you’re getting?

Sure, it’s:

Error:(984, 9) TS2304: Cannot find name ‘Handsontable’.

In Intellij, the “Handsontable” in the function you gave me is highlighted in red.

Also, things that have been added to the “Pro” version are missing, like Handsontable.Options.hiddenRows.

Hi @fisher23,

Like @chris said we are not currently supporting Type definitions for TypeScript. We have added this feature to our todo list (#2765, https://github.com/handsontable/handsontable/wiki/Feature-requests).

Please stay updated to this issue for future changes.