Skip to content
This repository has been archived by the owner on Nov 24, 2018. It is now read-only.

Unable to validate multiselect dropdowns #101

Closed
balwaan1986 opened this issue Mar 3, 2016 · 2 comments
Closed

Unable to validate multiselect dropdowns #101

balwaan1986 opened this issue Mar 3, 2016 · 2 comments

Comments

@balwaan1986
Copy link

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
Copy link
Collaborator

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
Copy link
Author

Yup.. this is perfect.. Thank you!

# for free to subscribe to this conversation on GitHub. Already have an account? #.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants