ui-select icon indicating copy to clipboard operation
ui-select copied to clipboard

required or ng-required not working with ui-select 'multiple' option

Open vinayagarwal opened this issue 11 years ago • 15 comments

required or ng-required=true is working fine with ui-select when the control is expected to select single value. However, with 'multiple' option, it is not working. The model presumes the value as array [], and marks it as valid=true.

Am I the only one facing this issue? Is this issue already addressed in the next version?

vinayagarwal avatar May 11 '15 11:05 vinayagarwal

I found this solution here

angular.module('common').directive('uiSelectRequired', function () { return { require: 'ngModel', link: function (scope, elm, attrs, ctrl) { ctrl.$validators.uiSelectRequired = function (modelValue, viewValue) {
            var determineVal;
            if (angular.isArray(modelValue)) {
                determineVal = modelValue;
            } else if (angular.isArray(viewValue)) {
                determineVal = viewValue;
            } else {
                return false;
            }

            return determineVal.length > 0;
        };
    }
};

});

add ui-select-required

imaa avatar May 11 '15 16:05 imaa

Thanks imaa! It is working well. The error msg also started showing up when I change the condition from: ng-show="abcForm.tasks.$error.required" to ng-show="abcForm.tasks.$invalid"

Wondering, can this be merged with the main repository or with ng-required itself with more sophistication to handle $error.required as well?

vinayagarwal avatar May 13 '15 05:05 vinayagarwal

ng-required or only required not work with "<ui-select multiple tagging" for me too. Component in form validation not detect state $scope.myform.$valid or $scope.myform.$invalid.

frekele avatar Jun 09 '15 08:06 frekele

I confirm the behaviour - when multiple is set required is not working.

evdoks avatar Jun 11 '15 10:06 evdoks

I just submitted a PR to fix this issue. It will allow you to just put required directly on the multiple tag and receive the expected functionality.

PR Link: https://github.com/angular-ui/ui-select/pull/1025

jcolemorrison avatar Jun 18 '15 23:06 jcolemorrison

That's great Cole. Thank you.

vinayagarwal avatar Jun 19 '15 09:06 vinayagarwal

The solution above works for me as well. Thanks!

michal-filip avatar Jul 08 '15 14:07 michal-filip

@improvisio how did you get the solution above working? I added the module then added ui-select-required in the ui-select tag but its not working.

GilbertChan avatar Jul 15 '15 21:07 GilbertChan

@GilbertChan I first added the directive as shown above: https://github.com/angular-ui/ui-select/issues/914#issuecomment-100962567 Then it worked. ui-select-required is not part of the ui-select package.

michal-filip avatar Jul 16 '15 07:07 michal-filip

Thanks, @imaa it works!

japharr avatar Aug 04 '15 10:08 japharr

@jcolemorrison

Check only required param doesn't work for me if i set ng-required="true", but code below works fine

if (angular.isDefined(attrs.multiple) && (angular.isDefined(attrs.required) ||  angular.isDefined(attrs.ngRequired)) ) {

dudapiotr avatar Sep 08 '15 12:09 dudapiotr

My solution

angular.module('angularApp')
  .directive('uiRequired', function() {
    return {
      require: 'ngModel',
      link: function(scope, elm, attrs, ctrl) {
        ctrl.$validators.required = function(modelValue, viewValue) {
          return !((viewValue && viewValue.length === 0 || false) && attrs.uiRequired === 'true');
        };

        attrs.$observe('uiRequired', function() {
          ctrl.$setValidity('required', !(attrs.uiRequired === 'true' && ctrl.$viewValue && ctrl.$viewValue.length === 0));
        });
      }
    };
  });

macem avatar Nov 21 '15 08:11 macem

@macem Thanks alot that helped

CLOUGH avatar Feb 05 '16 17:02 CLOUGH

@macem Thanks as well, I've just used it and it works perfectly!

Avien avatar Feb 09 '16 14:02 Avien

Resolved by #1492

user378230 avatar Mar 16 '16 14:03 user378230