繁体   English   中英

Angularjs验证ui-select倍数

[英]Angularjs validation for ui-select multiple

情况:

我有一个发送电子邮件的角度应用程序。 有一个包含三个字段的表单:地址 - 主题 - 文本。

对于地址字段,我使用角度ui-select

一切都运行正常,除了地址字段上的验证(主题和文本验证工作正常)。

编辑:

从版本0.16.1修复此错误。 正如@yishaiz所指出的那样。

因此,这个问题及其相关解决方案将ui-select版本视为<0.16.1。


代码:

HTML:

 <form name="emailForm" ng-submit="submitForm(emailForm.$valid)"> 

    <div class="row form-group">

        <label class="col-sm-2 control-label">To: </label>

        <div class="col-sm-10">

            <ui-select multiple ng-model="database_people.selectedPeople" theme="select2" ng-disabled="disabled" style="width:100%">

              <ui-select-match placeholder="Select person...">{{$item.name}} &lt; {{$item.value}} &gt;</ui-select-match>

              <ui-select-choices repeat="person2 in list_people | filter: {name: $select.search.name, value: $select.search.value, db_data_type_id: 5}">

                  <div ng-bind-html="person2.name | highlight: $select.search"></div>

                    <small>

                        email: <span ng-bind-html="''+person2.value | highlight: $select.search"></span>

                    </small>

              </ui-select-choices>

            </ui-select>

        </div>

    </div>

    <div class="col-sm-8">

         <button type="submit" class="btn btn-primary">Send</button>

    </div>

</form>

ANGULARJS:

$scope.submitForm = function(isValid) 
 {
    if (isValid) {
        alert('valid');
    }
    else {
        alert('not valid')
    }
};


PLUNKER:

http://plnkr.co/edit/MYW7SM9c9anH6RTomfRG?p=preview

正如您所见,ui-select是必需的,但表单被解析为有效。


问题(S):

如何使ui-select多个必需?

由于角度为#1.3,这样做的角度方法是创建自定义(验证)指令。

指示:

angular.module('myApp').directive('requireMultiple', function () {
    return {
        require: 'ngModel',
        link: function postLink(scope, element, attrs, ngModel) {
            ngModel.$validators.required = function (value) {
                return angular.isArray(value) && value.length > 0;
            };
        }
    };
});

代码变为:

<ui-select name="test"
           multiple 
           require-multiple  
           ng-model="database_people.selectedPeople" ...>
  <ui-select-match placeholder="Select person...">...
  </ui-select-match>
  <ui-select-choices ...>
        ...
  </ui-select-choices>
</ui-select>

改编自这个解决方案

PS :如果验证失败,您还可以使用ng-messages正确显示错误。 例:

<div ng-messages="emailForm.test.$error" >
  <p ng-message="required">Custom message here</p>
</div>

这是一个有效的Plunker

我改变的是这样的:

<form name="emailForm" ng-submit="submitForm(multipleDemo.selectedPeople.length > 0)"> 

它不使用$ valid形式,这是我想你最想做的事情。

我尝试使用此处概述的推荐方式https://docs.angularjs.org/api/ng/directive/form

<form name="emailForm" ng-submit="submitForm(emailForm.test.$valid)"> 

...

<ui-select multiple required ng-model="multipleDemo.selectedPeople" theme="select2" ng-disabled="disabled" style="width: 800px;" name=test>

但它不起作用。

我想知道问题是否由于multipleDemo.selectedPeople总是有效,即使它是一个空数组。

编辑:要验证多个字段,您可以执行此操作

<form name="emailForm" ng-submit="submitForm()"> 

在控制器中

  $scope.submitForm = function() 
     {
      var valid = true;
      if ($scope.multipleDemo.selectedPeople.length === 0) {
        valid = false;
      }
      // Followed by tests on other fields


      if (valid) {
          alert('valid');
      }
      else {
        alert('not valid')
      }


    };

Plunker

试试这个:如果ng-model的长度,即'storage'为'0',则显示错误消息,你也可以使用form form_name。$ submit显示错误消息。 这是处理多个选择所需字段的简单方法。

<form name="myform" ng-submit="!storage.length>0 && functionname()">
    <div class="form-group">
        <ui-select multiple ng-model="storage" name="weekdays">
            <ui-select-match>
                {{$item.name}}
            </ui-select-match>
            <ui-select-choices>
                {{weekday.name}}
            </ui-select-choices>
        </ui-select>
        <div ng-show="myform.$submitted">
           <div ng-show="!storage.length>0" >
               <span style="color:red;">Storage is required</span>
           </div>
        </div>
    </div>
    <button type="submit">Click</button>  
</form>

他们已经修复了ui-select版本0.16.1的错误。 看到这个工作的plunker 我改变的一件事就是这里的ui-select版本

  <script src="https://cdnjs.cloudflare.com/ajax/libs/angular-ui-select/0.16.1/select.js"></script>

这是 github关闭的bug问题。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM