【问题标题】:No validation does not work when adding multiple添加多个时没有验证不起作用
【发布时间】:2019-04-28 10:04:05
【问题描述】:

我正在尝试使用选项多个选项来验证 ui-select。 但是没有角度验证有效。甚至创建自定义验证。那总是返回无效。

仅删除所有验证工作的(多个)选项(必需和自定义)

<ui-select multiple ng-model="Model.Test" close-on-select="false"
           search-enabled="true" required custom-validation>
    <ui-select-match allow-clear="true">{{$item.Name}}</ui-select-match>
    <ui-select-choices repeat="item in Items | filter:$select.search">
        <div ng-bind-html="item.Name | highlight: $select.search"></div>
    </ui-select-choices>
</ui-select>
app.directive('customValidation', function () {
    return {
        require: 'ngModel',
        link: function (scope, elm, attrs, ctrl) {
            ctrl.$validators.customValidation = function (modelValue, viewValue) {
                return false;
            };
        }
    };
});

https://codepen.io/anon/pen/BEboLq

【问题讨论】:

  • 当询问由您的代码引起的问题时,如果您提供人们可以用来重现问题的代码,您将获得更好的答案。见How to create a Minimal, Complete, and Verifiable example。
  • 您的 codepen 与问题中的代码不匹配。你没有提到你正在使用jcs-autovalidate.js。

标签: angularjs ui-select


【解决方案1】:

那总是返回无效的。

如果验证器返回true,效果会更好:

app.directive('customValidation', function () {
    return {
        require: 'ngModel',
        link: function (scope, elm, attrs, ctrl) {
            ctrl.$validators.customValidation = function (modelValue, viewValue) {
                ̶r̶e̶t̶u̶r̶n̶ ̶f̶a̶l̶s̶e̶;̶
                return true;
            };
        }
    };
});

【讨论】:

  • 这只是一个示例,即使总是返回无效。它不会显示为无效。这不是最终代码。这是演示问题所在的示例代码。
  • @RudáCunha 在我的测试中false 肯定显示无效。您使用的是哪个版本的 AngularJS?什么版本的angular-sanitize.js? ui-select/dist/select.js 是什么版本的?
  • 在示例中它只对第二个选择有效,第一个总是返回有效。 codepen.io/anon/pen/BEboLq
猜你喜欢
  • 1970-01-01
  • 2016-02-18
  • 1970-01-01
  • 2013-10-06
  • 2020-12-19
  • 2021-12-09
  • 2020-04-14
  • 2017-07-12
  • 1970-01-01
相关资源
最近更新 更多