【问题标题】:Intercepting laravel validation errors in angularjs拦截angularjs中的laravel验证错误
【发布时间】:2017-07-08 21:07:10
【问题描述】:

我目前正在使用 Angular js 前端并使用 Laravel 作为后端 api 为我的网站制作联系表格。 这是我作为字段的一个示例。

<div class="row">
   <div class="form-group col-xs-12 floating-label-form-group">
      <label for="title">Your Name*</label>
      <input class="form-control" ng-model="contact_name" type="text" name="contact_name" placeholder="Your Name*">
      <span ng-if="errors.contact_name[0]" class="alert-danger">@{{ errors.contact_name.toString()}}</span>
   </div>
</div>

这会通过我的控制器进行调用。

$scope.processForm = function ()
{
    $scope.submissionSuccess = false;
    $scope.errors = null;
    $http({
        method:'POST',
        url:'/api/post-form',
        data:{
            _token: $scope._token,
            contact_name:$scope.contact_name,
            contact_email:$scope.contact_email,
            phone:$scope.phone,
            message:$scope.message
        }
    }).success(function(data, status, headers, config){

        /*CLEAR FORM FIELDS*/
        $scope.submissionSuccess = true;
        $scope.contact_name = '';
        $scope.contact_email = '';
        $scope.phone = '';
        $scope.message = '';

        // SHOW SUCCESS FOR 2 SECONDS.
        $timeout(function () {
            $scope.submissionSuccess = false;
        }, 2000);

    }).error(function(error){
        $scope.errors = error.data;
    });
}

然后将其传递给我的 laravel 控制器。 当我将数据记录到控制台时,我得到了。

对象 错误 : 目的 联系电子邮件 : 数组[1] 0 : “联系电子邮件字段是必需的。” 长度 : 1 原型 : 数组[0] 联系人姓名 : 数组[1] 0 : “联系人姓名字段是必填项。” 长度 : 1 原型 : 数组[0] 信息 : 数组[1] 0 : "消息字段是必需的。"

如何显示错误消息?

【问题讨论】:

    标签: php angularjs laravel


    【解决方案1】:

    也许你可以这样尝试。

    <span class="has-error" ng-repeat="error in errors.contact_name">{{ error }}</span>
    

    希望对您有所帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-08-15
      • 1970-01-01
      • 2012-05-09
      • 1970-01-01
      • 1970-01-01
      • 2017-09-20
      • 2018-12-27
      • 2016-04-29
      相关资源
      最近更新 更多