【问题标题】:Angularjs validation for ui-select multipleui-select multiple的Angularjs验证
【发布时间】:2015-01-29 05:42:59
【问题描述】:

情况:

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

对于地址字段,我使用的是 angular ui-select

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

编辑:

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

所以这个问题及其相关解决方案是关于 ui-select 版本


代码:

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 是必需的,但表单被解析为有效。


问题:

如何使 ui-select multiple 成为必需?

【问题讨论】:

    标签: javascript angularjs validation select ui-select


    【解决方案1】:

    他们已经修复了 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>
    

    还有this isgithub 关闭的bug 问题。

    【讨论】:

    • 好的。谢谢你让我知道。我已在问题中包含此信息。
    【解决方案2】:

    试试这个: 如果 ng-model 的长度,即“storage”为“0”,则显示错误消息,您也可以使用表单 form_name.$submitted 显示错误消息。这是处理多选必填字段的简单方法。

    <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>
    

    【讨论】:

      【解决方案3】:

      从 angular#1.3 开始,使用 angular 的方式是创建一个自定义(验证)指令。

      指令:

      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>
      

      改编自this solution

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

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

      【讨论】:

      • 像魅力一样工作!谢谢!
      • 最好的。我也为选择创建了另一个。早些时候我在控制器中使用手表。现在我可以在任何视图中使用它,不需要在每个控制器中添加额外的代码。
      • 很好的解决方案!虽然在我的情况下,我必须为此指令设置更高的优先级(例如 1000),否则链接功能甚至不会被执行。
      【解决方案4】:

      这是一个有效的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>
      

      但它不起作用。

      我想知道问题是否归结为 multiDemo.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

      【讨论】:

      • 非常感谢@camden_kid。这是一个很好的解决方案。如果我必须验证多个字段,我该怎么办?
      猜你喜欢
      • 2015-05-17
      • 2015-05-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-04-19
      • 1970-01-01
      相关资源
      最近更新 更多