Fra*_*ssi 5 javascript validation select angularjs ui-select
情况:
我有一个发送电子邮件的角度应用程序.有一个包含三个字段的表单:地址 - 主题 - 文本.
对于地址字段,我使用角度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}} < {{$item.value}} ></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>
Run Code Online (Sandbox Code Playgroud)
ANGULARJS:
$scope.submitForm = function(isValid)
{
if (isValid) {
alert('valid');
}
else {
alert('not valid')
}
};
Run Code Online (Sandbox Code Playgroud)
PLUNKER:
http://plnkr.co/edit/MYW7SM9c9anH6RTomfRG?p=preview
正如您所见,ui-select是必需的,但表单被解析为有效.
问题(S):
如何使ui-select多个必需?
nic*_*nel 11
由于角度为#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;
};
}
};
});
Run Code Online (Sandbox Code Playgroud)
代码变为:
<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>
Run Code Online (Sandbox Code Playgroud)
改编自这个解决方案
PS:如果验证失败,您还可以使用ng-messages正确显示错误.例:
<div ng-messages="emailForm.test.$error" >
<p ng-message="required">Custom message here</p>
</div>
Run Code Online (Sandbox Code Playgroud)
这是一个有效的Plunker.
我改变的是这样的:
<form name="emailForm" ng-submit="submitForm(multipleDemo.selectedPeople.length > 0)">
Run Code Online (Sandbox Code Playgroud)
它不使用$ valid形式,这是我想你最想做的事情.
我尝试使用此处概述的推荐方式https://docs.angularjs.org/api/ng/directive/form
<form name="emailForm" ng-submit="submitForm(emailForm.test.$valid)">
Run Code Online (Sandbox Code Playgroud)
...
<ui-select multiple required ng-model="multipleDemo.selectedPeople" theme="select2" ng-disabled="disabled" style="width: 800px;" name=test>
Run Code Online (Sandbox Code Playgroud)
但它不起作用.
我想知道问题是否由于multipleDemo.selectedPeople总是有效,即使它是一个空数组.
编辑:要验证多个字段,您可以执行此操作
<form name="emailForm" ng-submit="submitForm()">
Run Code Online (Sandbox Code Playgroud)
在控制器中
$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')
}
};
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
15164 次 |
| 最近记录: |