allowEmpty doesn't work properly

Tags: #<Tag:0x00007efc6b5d81a8> #<Tag:0x00007efc6b5e3f58>

I noticed that the allowEmpty property is not working properly. I know that you need a validator for it to work, but it fails. Check out this fiddle

https://jsfiddle.net/ezmkw8ng/1/

document.addEventListener("DOMContentLoaded", function() {

  var bogus_data = [
      ['asd', "2015-10-2010"], // an invalid date here
      ['', '']
    ],
    example1 = document.getElementById('table'),

    settings1 = {
      data: bogus_data,
      colHeaders: ['name', 'Date'],
      columns: [{
          allowEmpty: true,
          validator: (value, callback) => {
            const valid = value === 'ok';
            callback(valid);
	  },
          type: 'text'
        },
        {
          type: 'date',
          allowEmpty: true,
          dateFormat: 'YYYY-MM-DD'
        }
      ]
    };
  var hot = new Handsontable(example1, settings1);
  hot.validateCells(function(valid) {})
});

The funny thing is, if I set allowEmpty to false for the date, it works there, but it shouldn’t with no validator, right? :confused:

Hi @anthony.gls

I think it’s working correctly. If you have type of the column set up, then it autmatically attatches default validator, so creating a custom one is not necessary. This is what documentation says:

To use the allowEmpty option, you need to set the validator option (or the type option).

Then this should work, but doesn’t… https://jsfiddle.net/zfeu1msx/

settings1 = {
      data: bogus_data,
      colHeaders: ['name', 'Date'],
      columns: [{
          allowEmpty: true,
          validator: (value, callback) => {
            const valid = value === 'ok';
            callback(valid);
					},
        },
        {
          type: 'date',
          allowEmpty: true,
          dateFormat: 'YYYY-MM-DD'
        }
      ]
    };

Honestly, it should just kinda do what it says :grimacing:

Hi @anthony.gls

If the custom validator is present, then it works. You can see it here: https://jsfiddle.net/handsoncode/r4edcfyL/ I modified your example a bit, so the validator expects the value asd and treat this cell as correct, but with allowEmpty: false it shows that the empty cell is incorrect.

Thank you for this issue, as because of this we discovered, that the information that only cell type is obligatory for make it work is not true in case of text cell type, as it doesn’t have any default validator.

OK - so now change allowEmpty back to true and it’s an error again. Basically allowEmpty does not really work intuitively. It should override any validator rules if the value is empty. Otherwise if I need to specify that empty values are valid in my custom validator, then allowEmpty only works when there is no validator.

I can agree that its behavior is not intuitive. We should consider it when we will do the revision of the text cell type.

1 Like