验证复选框角度

时间:2016-04-19 09:05:59

标签: angularjs validation checkbox

如何验证此复选框,必须至少检查一个,如果没有,则必须有一些消息或警告

<div class="form-group" ng-class="{ 'has-error' : actionsAddForm.active.$invalid && !actionsAddForm.active.$pristine }">
    <label class="control-label col-sm-2">Days*</label>
    <div class="col-sm-10">
        <label>Monday
          <input type="checkbox" ng-model="actions.value1">
        </label>
        <label>Tuesday
          <input type="checkbox" ng-model="actions.value2">
        </label>
        <label>Wednesday
          <input type="checkbox" ng-model="actions.value3">
        </label>
        <label>Thursday
          <input type="checkbox" ng-model="actions.value4">
        </label>
        <label>Friday
          <input type="checkbox" ng-model="actions.value5">
        </label>
        <label>Saturday
          <input type="checkbox" ng-model="actions.value6">
        </label>
        <label>Sunday
          <input type="checkbox" ng-model="actions.value7">
        </label>
    </div>

1 个答案:

答案 0 :(得分:1)

可以这样做:

<div ng-hide="actions.value1 || actions.value2 || actions.value3...">
    You have an error 
</div>

否则,如果您希望能够使用$ valid,...您必须创建一个继承自ngModelController的自定义指令,并在其中添加自定义验证器:https://docs.angularjs.org/api/ng/type/ngModel.NgModelController