我目前正在爲我的網站使用角度js前端和使用Laravel作爲後端api的聯繫表單。 這是我作爲一個領域的例子。在angularjs中攔截laravel驗證錯誤
<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控制器。 當我將數據記錄到控制檯時,
對象 錯誤 : 對象 CONTACT_EMAIL : 陣列[1] : 「是必需的聯繫人的電子郵件字段」。 長度 : 原 : 數組[0] CONTACT_NAME : 陣列[1] : 「聯繫人姓名字段是必需的。」 長度 : 原 : 數組[0] 消息 :[1] 陣列: 「是必需的消息字段」。
我將如何顯示錯誤消息?明確說聯繫電子郵件字段是必需
你好,我知道這些字段是空的。我不想將其發佈到數據庫/。我試圖在前端顯示驗證錯誤作爲跨度。所以被記錄的消息我想輸出它旁邊的approrpiate字段。 –
現在試試我已經更新了我的答案 –