【发布时间】:2015-05-07 13:47:37
【问题描述】:
我有一个页面,其中有一个名为配置文件更新的表单,当用户访问此页面以更新他的配置文件时,他只更新 1 或 2 个字段,因此每次表单检查表单是否有效或然后不仅保存在数据库中,而且如果密码和确认密码字段未更新,我想保存然后更新他的个人资料,而且当他想更改密码时,时间保存按钮将被禁用并在更新个人资料后检查验证.
表示当用户仅更新名称时,该时间也会更新配置文件,并且用户更新密码时首先检查验证,然后更新配置文件。
保存并isValid检查:
app.controller('DemoCtrl', function($scope, $http) {
$scope.saveProfile = function(){
if($scope.profile.$valid){
ngProgress.start();
user.saveProfile($scope.currentUser.details,function(response){
angular.copy(response,shared.data.currentUser);
notification.success($filter("il8n")("_ProfileUpdateMessage_"));
ngProgress.done();
});
}
}
$scope.isValidForm = function(){
//if(($scope.profile.confirmpassword && $scope.profile.newpassword && $scope.profile.newpassword.$modelValue !== $scope.profile.confirmpassword.$modelValue))
// return true;
if(!$scope.profile.$valid){
if(($scope.profile.confirmpassword && $scope.profile.newpassword && $scope.profile.confirmpassword.$modelValue && $scope.profile.newpassword.$modelValue && ($scope.profile.newpassword.$modelValue.length == 0 || $scope.profile.confirmpassword.$modelValue.length == 0))){
return false;
}
if(($scope.profile.confirmpassword && $scope.profile.newpassword && $scope.profile.confirmpassword.$modelValue && $scope.profile.newpassword.$modelValue && $scope.profile.newpassword.$modelValue === $scope.profile.confirmpassword.$modelValue)){
return false;
}
}
if($scope.profile.$valid && $scope.profile.$dirty)
return false;
if($scope.profile.newpassword.$invalid || $scope.profile.newpassword.$dirty && $scope.profile.confirmpassword.$invalid || $scope.profile.confirmpassword.$dirty){
//$scope.profile.newpassword.$valid;
//$scope.profile.confirmpassword.$valid;
return false;
}
//if($scope.profile.newpassword.empty && $scope.profile.confirmpassword.empty ){
// return true;
//}
return true;
}
});
找到 plnkr here。
【问题讨论】:
-
不确定我是否完全理解您的问题,但
ng-model-options="{allowInvalid: true}"会提供帮助吗? -
我的问题是当用户在没有输入密码字段的情况下更新他的个人资料时,如果他只更改名称字段,那么也会更新用户个人资料,并且当用户想要更改密码时,该时间检查验证和更新个人资料。
-
我正要编写我的个人资料页面。我将密码设置为 "" 并且仅在不为空时进行验证,如果它们匹配则验证将通过。如果它们不匹配,我正在使用将表单设置为无效的指令。
-
我尽量避免编码验证。我让指令将字段和表单设置为无效。我在 git 上找到了指令
标签: javascript angularjs