Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make a ui-select field as required?

Tags:

html

angularjs

I want to make the following field (settings) in my form as a required field in. How can I do it?

<div class="form-group">
    <label class="col-xs-5 control-label"> Settings*</label>

    <div class="col-xs-7">
        <ui-select multiple tagging="adPreferredEmailDomainPatternTransform" id="emailDomainPatternListInput"
                   tagging-tokens="SPACE|," theme="bootstrap"
                   ng-disabled="settings.enableAuthentication == 'false'"
                   ng-model="settings.emailDomainPatternList">
            <ui-select-match>{{$item.displayFormat}}</ui-select-match>
            <ui-select-choices repeat="item in emailDomainPatterns">
                {{item.displayFormat}}
            </ui-select-choices>
        </ui-select>
    </div>
</div>
like image 609
fatCop Avatar asked Mar 19 '15 10:03

fatCop


4 Answers

None of the answers really worked for me. I solved it using a simple hidden input with the same ng-model as the ui-select and validating that in the form.

<input type="hidden" name="email_domain_pattern" ng-model="settings.emailDomainPatternList" required/>
like image 69
Nirav Gandhi Avatar answered Oct 31 '22 14:10

Nirav Gandhi


You can write ng-required="true".

<div class="form-group">
    <label class="col-xs-5 control-label"> Settings*</label>

    <div class="col-xs-7">
        <ui-select multiple tagging="adPreferredEmailDomainPatternTransform"                 
                   id="emailDomainPatternListInput"
                   tagging-tokens="SPACE|," 
                   theme="bootstrap"
                   ng-disabled="settings.enableAuthentication == 'false'"
                   ng-model="settings.emailDomainPatternList"
                   ng-required="true">
            <ui-select-match>{{$item.displayFormat}}</ui-select-match>
            <ui-select-choices repeat="item in emailDomainPatterns">
                {{item.displayFormat}}
            </ui-select-choices>
        </ui-select>
    </div>
</div>
like image 35
barış çıracı Avatar answered Oct 31 '22 13:10

barış çıracı


I think that this is a known and long-standing bug. See this GitHub issue for more information.

like image 6
Kostas Rousis Avatar answered Oct 31 '22 14:10

Kostas Rousis


you can use custom directive.

angular.module("app").directive('uiSelectRequired', function () {
    return {
        require: 'ngModel',
        link: function (scope, element, attr, ctrl) {
            ctrl.$validators.uiSelectRequired = function (modelValue, viewValue) {
                if (attr.uiSelectRequired) {
                    var isRequired = scope.$eval(attr.uiSelectRequired)
                    if (isRequired == false)
                        return true;
                }
                var determineVal;
                if (angular.isArray(modelValue)) {
                    determineVal = modelValue;
                } else if (angular.isArray(viewValue)) {
                    determineVal = viewValue;
                } else {
                    return false;
                }
                return determineVal.length > 0;
            };
        }
    };
});
like image 3
ddagsan Avatar answered Oct 31 '22 13:10

ddagsan