netceteragroup / valdr

A model centric approach to AngularJS form validation
http://netceteragroup.github.io/valdr/
MIT License
153 stars 43 forks source link

Unable to validate multiselect dropdowns #101

Closed balwaan1986 closed 8 years ago

balwaan1986 commented 8 years ago

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?

marcelstoer commented 8 years ago

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?

balwaan1986 commented 8 years ago

Yup.. this is perfect.. Thank you!