【问题标题】:How to display server side validation error messages on form with angular js如何使用 Angular js 在表单上显示服务器端验证错误消息
【发布时间】:2015-05-05 09:25:51
【问题描述】:

我在我的项目中使用 AngularJS 和 Spring MVC。在此,我将 JSON 发送到其余控制器,该控制器验证字段并在验证失败的情况下返回错误对象,如下所示:

{"validationErrors":[
    {
        "errorCode":"emailId",
        "errorDescription":"Please enter email address."
    }
]}

现在,在jsp上如何显示内联错误信息?

我的jsp代码如下:

   <label ng-model="emailLbl" for="userEmailID">Email ID</label>
   <input type="email" ng-model="user.emailId" name="emailId" id="userEmailID" placeholder="Enter your email ID" />

            //Here, I want to display inline error message
</div> 

而我的 js 代码是: //更新 angular.module('MiniApp', []);

angular.module('MiniApp').controller('loginCtrl', ['$scope', '$http','$location',function($scope,$http,$location) {

    $scope.loginSubmit = function(user) {
   $scope.errors = [];
  $scope.jsonObject = angular.copy(user);
var data1;
setTimeout(function(){ 
    data1=document.getElementById("hiddenJson").value;
    $http({
        method: 'POST', 
        url: 'register1/login',
        headers: {'Content-Type': 'application/json'},
        data: data1
    }).
success(function(data, status, headers, config) {
    alert('Success:'+data.prospectResponseDetails.emailId+" - "+status);
    $location.path('WEB-INF/pages/login-step1.jsp');
}).
error(function(data, status, headers, config) {
    _showValidationErrors($scope, data.validationErrors);
    $scope.errors = data.validationErrors;

});

    $scope.getErrorMessage = function(errorCode) {
       var error;
       $scope.errors.forEach(function(error) {
         if(error.errorCode === errorCode) {
           error = error.errorDescription;
         }
       });
       return error;
    }
}, 200);

  };
}]);

【问题讨论】:

  • 应该学习一些角形教程

标签: javascript angularjs spring jsp spring-mvc


【解决方案1】:

根据您的代码:

验证错误捕获在:

error(function(data, status, headers, config) {
        _showValidationErrors($scope, data.validationErrors);
   }); 

所以不要使用_showValidationErrors,而是使用

$scope.errors = data.validationErrors;

现在在您的 HTML 中:

 <input type="email" ng-model="user.emailId" name="emailId" id="userEmailID" placeholder="Enter your email ID" />

    //Here, I want to display inline error message

   <ul ng-if="errors.length > 0">
      <li  ng-repeat="error in errors">{{error.errorCode}} {{error.errorDescription}}</li>
  </ul>
</div> 

重置错误:在你的

里面
$scope.loginSubmit = function(user) {

    $scope.errors = []; // reset errors to empty array

编辑://根据评论

显示特定的错误信息:

$scope.getErrorMessage = function(errorCode) {
   var error;
   $scope.errors.forEach(function(error) {
     if(error.errorCode === errorCode) {
       error = error.errorDescription;
     }
   });
   return error;
}

现在在您的 HTML 中:

<input type="email" ng-model="user.emailId" name="emailId" id="userEmailID" placeholder="Enter your email ID" />

<div ng-if="getErrorMessage('emailId')">
    {{getErrorMessage('emailId')}}
</div>

【讨论】:

  • 但是,现在的问题是,假设我在 jsp 上有两个字段,那么我应该如何在每个字段上显示错误消息?
  • 在这种情况下,您必须根据字段作为键发送 ValidationErrorMessages。像 {"email" : {"errorCode": "", "errorDescription" : ""}, "password": {}} 等。现在,您可以在每个字段下方检查errors.email(如果存在,则检查errors.email)。 errorCode 和 errors.email.errorDescription ;)
  • 实际上,根据业务需求,我想传递包含 errorCode 和 ErrorDescription 的错误对象列表,例如:
  • {"validationErrors":[{"errorCode":"emailId","errorDescription":"请输入电子邮件地址。","errorCode":"Password","errorDescription":"请输入密码"}]} 我怎样才能用这样的 JSON 来做到这一点
  • 太棒了。在这种情况下,创建一个函数并传递 errorCode。如果存在错误代码,则显示消息。
猜你喜欢
  • 2020-12-26
  • 2017-03-03
  • 2021-03-29
  • 1970-01-01
  • 2016-03-05
  • 1970-01-01
  • 2019-12-10
  • 2012-02-15
  • 1970-01-01
相关资源
最近更新 更多