我有一个指令,根据表单的$valid和$untouched属性验证表单中的输入--如果输入被“触摸”,它会检查验证,并相应地将字体和边框用红色/绿色表示,如果输入没有“被”“触摸”,则不会做任何事情。
我使用ngBootBox的自定义对话框,所以我没有type="submit"类型的按钮来提交表单,而是使用"Create“按钮的回调函数来传递/保存数据。
我的问题是,当我单击"create“按钮而表单不”有效“时,因为其中一些字段是空的--我的输入仍然是”未触及“的,因此不会调用$watch函数。有什么解决办法吗?有什么方法可以这样做:$scope.createProjectForm.$setTouched(true);将使该形式的所有子输入获得该值吗?
我也试过了,但没用:
angular.forEach($scope.createProjectForm, function(field){
field.$setTouched(true);
});这是我的验证指令:
angular.module('mean.theme').directive("inputValidation", function () {
return {
restrict: 'EA',
require: 'ngModel',
link: function (scope, inputElement, attrs, ngModelCtrl) {
var $icon = $('<i class="fa"></i>');
inputElement.before($icon);
scope.$watchGroup([
function(){
return ngModelCtrl.$untouched;
},
function(){
return ngModelCtrl.$valid;
}
], function(Paramaters){
console.log(scope);
if(!Paramaters[0]) {
if (Paramaters[1]) {
inputElement.switchClass('validation-warning-border','validation-success-border',50)
inputElement.prev().switchClass('fa-warning validation-warning-font' , 'fa-check validation-success-font',50);
} else {
inputElement.switchClass('validation-success-border','validation-warning-border',50)
inputElement.prev().switchClass('fa-check validation-success-font','fa-warning validation-warning-font',50)
}
}
});
}
};
});这是我的控制器代码的一部分:
$scope.create = function () {
var options = {
title: 'Create',
templateUrl: 'project.html',
scope: $scope,
buttons: {
warning: {
label: "Cancel",
className: "btn-link",
callback: function () {
}
},
success: {
label: "Create",
className: "green",
callback: function () {
if ($scope.createProjectForm.$valid){
$scope.createProject(template);
} else {
$scope.project.createButtonClicked = true;
return false;
}
}
}
}
};
$ngBootbox.customDialog(options);
};这是我HTML代码的一部分:
<form role="form" name="createProjectForm">
<label>
Name Your Project
</label>
<div>
<input type="text" name="project.name"
ng-model="project.name" required="required" class="form control" input-validation/>
</div>
<label>
Name Your Project
</label>
<div>
<input type="text" name="project.title"
ng-model="project.title" required="required" class="form control" input-validation/>
</div>
</form>我找到了我所需要的,更简单、更短的方法:
可以手动设置:$scope.createProjectForm.$setSubmitted()到true
然后,为该更改设置子(输入) $watch:
scope.$watchGroup([
function(){
return ngModelCtrl.$untouched;
},
function(){
return ngModelCtrl.$valid;
},
function(){
return ngModelCtrl.$$parentForm.$submitted;
}
], function(Paramaters){
// code here
}发布于 2015-12-08 20:04:26
您可以采用类似于此的格式:
if ($scope.form.$invalid) {
angular.forEach($scope.form.$error, function (field) {
angular.forEach(field, function(errorField){
errorField.$setTouched();
});
});
}见'$setTouched':https://docs.angularjs.org/api/ng/type/ngModel.NgModelController
发布于 2017-02-08 12:29:02
对于嵌套表单:
function setFormTouched(form) {
// Check if the form/property has the $setSubmitted method
if (form.hasOwnProperty('$submitted')) {
// Iterate through each of the required error properties
angular.forEach(form.$error, function (errorType) {
// Iterate through each error type
angular.forEach(errorType, function (prop) {
// Check if the property has the $setTouched method
if (prop.hasOwnProperty('$touched')) prop.$setTouched();
// Recursive call to handle nested forms
setFormTouched(prop);
});
});
}
}解决方案基于Patrick的码页,但是我不得不编辑原始代码,因为hasOwnProperty不适用于原型中定义的函数。例如:
if(prop.hasOwnProperty('$setTouched'))都是假的。
编辑:我最初需要的是一种使用户更容易在嵌套选项卡中查找错误的方法。这是用于禁用/启用submit按钮和错误消息的最后工作范围函数:
$scope.isFormValid = function (topLevelForm) {
if (topLevelForm.$valid) {
return true;
}
// Form not valid, triggering fields to make it easier to find errors
setFormTouched(topLevelForm);
function setFormTouched(form) {
// Check if the form/property has the $setSubmitted method
if (form.hasOwnProperty('$submitted')) {
// Iterate through each of the required error properties
angular.forEach(form.$error, function (errorType) {
// Iterate through each error type
angular.forEach(errorType, function (prop) {
// Check if the property has the $setTouched method
if (prop.hasOwnProperty('$touched')) prop.$setTouched();
// Recursive call to handle nested forms
setFormTouched(prop);
});
});
}
}
return false;
};https://stackoverflow.com/questions/34164122
复制相似问题