netceteragroup/valdr

Unable to validate multiselect dropdowns

balwaan1986 opened this issue · 2 comments

I'm using ui-select for dropdowns and valdr required field validation works like a charm for single select. However, it does not work for multiselect dropdowns.

I figured out the problem was due to valdrUtil.notEmpty method. Here it is checking if the value is undefined, empty string or null value. For a multiselect dropdown, it will be an empty array.

So we can fix this by adding a check to see if the value is an array and if yes, check its length property. If its 0, it means its an empty array and hence should return false.

notEmpty: function (value) {
  if (this.isNaN(value)) {
    return false;
  }
  if (angular.isArray(value) && value.length === 0){
    return false;
  }
  return angular.isDefined(value) && value !== '' && value !== null;
}

Is this something that can be included in the next release?

And the proper test would then be

it('should validate arrays', function () {
  expect(valdrUtil.notEmpty(['Apple', 'Banana'])).toBe(true);
  expect(valdrUtil.isEmpty(['Apple', 'Banana'])).toBe(false);
  expect(valdrUtil.isEmpty([])).toBe(true);
  expect(valdrUtil.notEmpty([])).toBe(false);
});

Agree?

Yup.. this is perfect.. Thank you!