Form validation - Required one of many in a group

后端 未结 8 1204
时光取名叫无心
时光取名叫无心 2020-12-05 18:31

In the project I\'m working on at the moment I currently have three textboxes and I need to validate that at least one of the text boxes has been populated.

I\'ve be

相关标签:
8条回答
  • 2020-12-05 19:34

    You can use ng-required to force the user to fill at least one field by checkingthe length attribute of the string.

    You can do the following for example:

    <form name="myForm">
                <input type="text" ng-model="fields.one" name="firstField" ng-required="!(fields.one.length || fields.two.length || fields.three.length)" />
                <br/>
                <input type="text" name="secondField" ng-required="!(fields.one.length || fields.two.length || fields.three.length)" ng-model="fields.two" />
                <br/>
                <input type="text" ng-model="fields.three" name="thirdField" ng-required="!(fields.one.length || fields.two.length || fields.three.length)" />
                <br/>
                <button type="submit" ng-disabled="!myForm.$valid">Submit</button>
    </form>
    

    See this working fiddle example for more details.

    You can have more details about required vs ng-required by reading this question

    0 讨论(0)
  • 2020-12-05 19:36

    I had similar grouping requirement in my project and I wrote this.Interested people can use this

    .directive('group',function(){
            return {
                require: '^form',
                link : function($scope,element,attrs,formCtrl){
                    var ctrls =[];
    
                    element.find(".group-member").each(function(){
                        var member = angular.element($(this));
                        var mdlCtrl = member.data("$ngModelController");
                        if(!mdlCtrl){
                            throw "Group member should have ng-model";
                        }
                        ctrls.push(mdlCtrl);
                    });
    
                    var errKey = attrs['name']+"GrpReqd";
                    var min = attrs['minRequired'] || 1;
                    var max = attrs['maxRequired'] || ctrls.length;
    
                    $scope.validateGroup = function(){
                        var defined=0;
                        for(i=0;i<ctrls.length;i++){
                            if(ctrls[i].$modelValue){
                                defined++;
                            }
                        }
                        if(defined < min || defined > max){
                            formCtrl.$setValidity(errKey,false);
                        } else {
                            formCtrl.$setValidity(errKey,true);
                        }
                    };
    
                    //support real time validation
                    angular.forEach(ctrls,function(mdlCtrl){
                        $scope.$watch(function () {
                              return mdlCtrl.$modelValue;
                           }, $scope.validateGroup);
                    });
    
                }
            };
        })
    

    HTML usage :

    <div name="CancellationInfo" group min-required="1" max-required="1">
                <input type="text" class="form-control group-member" style="width:100%;" name="Field1" ng-model="data.myField"  />
                <input type="text" class="form-control group-member" style="width:100%;" name="Field1" ng-model="data.myField2"  />
                <input type="text" class="form-control group-member" style="width:100%;" name="Field2" ng-model="data.myField3"  />
            </div>
    

    Here group directive identifies the logical grouping. This directive sits on an element without ng-model, a div in the above example. group directive receives 2 optional attribute min-required and max-required. Group members are identified using group-member class on individual fields. Group members are supposed to have an ng-model for binding. Since group directive doesn't have an ng-model error will be emitted under yourForm.$error.CancellationInfoGrpReqd in the above case. Unique Error key is generated from the element name on which group directive is sitting with GrpReqd appended to it.

    0 讨论(0)
提交回复
热议问题