我如何验证角度多选?

时间:2014-08-05 07:10:11

标签: angularjs bootstrap-modal angular-directive

  

我正在使用http://isteven.github.io/angular-multi-select/中的angular-multi-select。它根据我的需要工作。我正在使用模式形式的多选,我想验证它,但它不验证我何时使用所需的选项。任何人都可以建议我如何验证角度多选形式。

<div class="form-group" ng-class="{'has-error' :batch_form.end_time.required || batch_form.start_time.required || batch_form.days.$invalid && (!batch_form.days.$pristine || submitted) }">
    <label for="">Timings</label>
    <div ng-repeat="batch_timing in batch_timings" >
        <div class="row" ng-hide="batch_timing._destroy == 1">
             <div class="col-md-10">
                 <div class="row new-timings">
                    <div class="col-md-1">
                      <span class="text-style pull-left">Days</span>
                    </div>
                    <div class="timings-row" >
                      <div class="col-md-11">
                          {{batch_form.start_time.$error.required}}
                          <div 
                            multi-select
                            input-model="batch_timing.days"
                            output-model="resultData"
                            button-label="acronym"
                            item-label="acronym symbol name"
                            tick-property="ticked"
                            helper-elements="all none reset"
                            required name="days">
                          </div>  
                      </div>
                    </div>
                  </div>
                  <div class="row col-md-offset-1 new-timings">
                    <div class="col-md-1">
                      <span class="text-style">from</span>
                    </div>
                    <div class="col-md-4">
                      <select 
                        class="form-control"
                        data-ng-model="batch_timing.start_time" 
                        ng-options="timing as timing for timing in timings"
                        required name="start_time">
                      </select> 
                    </div>  
                    <div class="col-md-1">
                      <span class="text-style">to</span>
                    </div>
                    <div class="col-md-4">
                      <select class="form-control"
                            data-ng-model="batch_timing.end_time" 
                            ng-options="timing as timing for timing in timings"
                            required name="end_time">
                      </select>                         
                    </div> 

                  </div>
                </div>
                <div class="col-md-2">
                  <span class="delete-button">
                      <a ng-hide="batches.single.id" class="btn btn-danger" data-ng-model="batch_timing.id" href ng-click="batch_timings.splice($index, 1)"  ><i class="glyphicon glyphicon-trash"></i></a>
                      <a ng-show="batches.single.id" class="btn btn-danger" data-ng-model="batch_timing.id" href ng-click="remove(batch_timing.id, batch_timings)"  ><i class="glyphicon glyphicon-trash"></i></a>

                  </span>
                </div>      
              </div>   
            </div> 
            <div class="help-block col-lg-12 col-md-12 col-sm-12" ng-show="batch_form.days.$error.required && (!batch_form.start_date.$pristine || submitted)"> Start time and end time are required. 
            </div>
            <div class="help-block error" ng-show="batch_form.start_time.$error.required && batch_form.end_time.$error.required && (!batch_form.start_date.$pristine || submitted)"> Start time and end time are required. 
            </div>

            <div class="row">
              <div class="col-md-12">
                <a class="btn btn-primary" href ng-click="newTiming($event)"><i class="glyphicon glyphicon-plus"></i></a>
              </div>
            </div>  

   </div>

提前致谢

4 个答案:

答案 0 :(得分:8)

我刚刚使用了具有相同输出模型的不可见输入字段。并将其设置为display:none。

<div class="col-md-11">
{{batch_form.start_time.$error.required}}
<div multi-select 
  input-model="batch_timing.days" 
  output-model="resultData" 
  button-label="acronym" 
  item-label="acronym symbol name" 
  tick-property="ticked" 
  helper-elements="all none reset">
</div>

<input type="text" ng-model="resultData" name="days" style="display:none" required />

但请注意名称 - 只需从多选中删除name属性,然后将其添加到隐藏的输入字段中。

答案 1 :(得分:3)

扩展ilmgb回答:

如果您还想在验证错误的控件上添加验证类

<div multi-select 
  input-model="batch_timing.days" 
  output-model="resultData" 
  button-label="acronym" 
  item-label="acronym symbol name" 
  tick-property="ticked" 
  helper-elements="all none reset"
  class="resultDataSelect"
  >


<input type="text" ng-model="resultData" name="days" style="display:none" required />
<div ng-show="myForm.days.$error.required" class="validation-error">Days are required</div>

在控制器中添加此监视,以便在输出模型更改时附加类:

$scope.$watch(function (scope) { return $scope.resultData },
           function (newValue, oldValue) {

               if (newValue.length <= 0) {
                   $(".resultDataSelect").find(".multiSelectButton").addClass("ng-invalid");
               } else {
                   $(".resultDataSelect").find(".multiSelectButton").removeClass("ng-invalid");
               }
           });

答案 2 :(得分:0)

使用此output-model="resultData",您将从多选中获取所选项目的列表。

在视图中:

$scope.resultData = [];

$scope.validation = function () {
	if($scope.resultData === null || $scope.resultData == ''){
    	false //not validated
    }else {
    	true //validated
    }
}
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div 
	multi-select
	input-model="batch_timing.days"
	output-model="resultData"
    button-label="acronym"
    item-label="acronym symbol name"
    tick-property="ticked"
    helper-elements="all none reset"
    required name="days">
</div>
<button ng-click="validation()">submit</button>
 

只检查输出列表是否为空。

希望它有所帮助,如果确实如此,请按照答案进行检查。祝好运! ;)

答案 3 :(得分:0)

这个已接受的答案对我不起作用。用所需模型的长度(如果它是数组)替换所需模型。

这应适用于使用数组的任何人。

   <div ng-dropdown-multiselect options="ctrl.list" selected-model="ctrl.item"></div>
   <input type="hidden" ng-model="ctrl.item[0]" ng-required="true">