如果 Angular 应用程序正确验证客户端表单,Angular 应用程序是否需要服务器端错误处理?
Is server side error handling necessary for Angular app if Angular app is properly validating the client form?
例如,我有这样的HTML:
<form novalidate>
<input type="text" name="name" required />
<button type="submit" ng-submit="saveThis()"> Save </button>
</form>
我正在使用 Angular 处理错误 required
。
现在在服务器 post 方法上:
router.post('/', function(req, res, next) {
req.checkBody('name', 'name is required.').notEmpty();
var errors = req.validationErrors();
if(errors) {
//Send these errors to angular
} else {
//Code for saving data
}
}
我的问题是:
我已经在处理 Angular 中的错误。那么服务器端验证的必要性是什么?
如果你说如果Javascript在浏览器中被禁用,那么我需要服务器端错误。但是我的表格将永远不会被 posted 因为我使用 ng-submit
从 Angular 到 post 数据因为 Angular 如果 [=66] 也将不起作用=] 已禁用。
好吧,你可能会说,如果我的服务器从另一个应用程序收到请求,那么我认为这是必要的。如果您知道任何其他例外情况,请告诉我。但让我们回到我的应用程序范围:
如果需要将服务器错误发送到我的 Angular 应用程序,那么你能帮我将错误消息从服务器发送到 Angular 然后在 [=57= 上显示吗? ] 页?
更新:@paqash
请求的代码
在我的控制器中:
$scope.submit = function(effect) {
effectService.save(effect, function(err, result, status) {
$scope.effect = null;
if(!(effect == null || effect == undefined)) {
if(!(effect._id == undefined || effect._id == '')) {
$scope.currentEffectName = null;
$location.path('/effect');
}
}
$setPristine();
$setUntouched();
});
}
在我的服务中:
function save(effect, callback) {
if(effect == undefined || effect._id == undefined || effect._id == '') {
$http.post('/api/effect', effect);
} else {
$http.put('/api/effect/' + effect._id, effect);
}
}
更新2:
这是我的 HTML:
<div class="col-md-10">
<ul class="errors" ng-if="errors.length > 0">
<li class="alert alert-danger" ng-repeat="error in errors"> {{error.msg}} </li>
</ul>
</div>
控制器保存功能如下:
$scope.submit = function(effect) {
effectService.save(effect, function(err, result, status) {
if(err) {
$scope.errors = err;
}
else {
$scope.errors = null;
}
...........
});
}
这是服务:
function save(effect, callback) {
if(effect == undefined || effect._id == undefined || effect._id == '') {
$http.post('/api/effect', effect).then(saveSuccessCallback, saveFailureCallback);
} else {
$http.put('/api/effect/' + effect._id, effect).then(saveSuccessCallback, saveFailureCallback);
}
}
function saveSuccessCallback() {
//Clear errors
return true;
}
function saveFailureCallback() {
//Raise errors
return false;
}
我删除了所有用于检查服务器端验证的客户端验证。现在,当我在没有在输入框中输入任何内容的情况下单击“保存”按钮时,我没有看到任何错误。
需要服务器端验证,是的。你永远不能相信客户。
您可以通过以下方式发送这些错误:
res.status(400).send({errors: errors});
然后在您的 saveThis
方法中检查响应的 http 代码,如果有错误,则显示它们。
编辑:您的 $http 方法可以与成功和错误回调链接在一起,如下所示:
$http.post('/someUrl', data, config).then(successCallback, errorCallback);
添加 errorCallback 方法后,当您的状态不是 200-something 时它会触发,您可以在那里检查响应对象的状态和错误数据。
更多信息here。
@vishal 更新(操作):
这是我从服务传递响应对象的方式:
function save(effect, callback) {
if(effect == undefined || effect._id == undefined || effect._id == '') {
$http.post('/api/effect', effect)
.then(function (response) {
//Clear errors
callback(response);
}, function(response) {
//Raise errors
callback(response);
});
} else {
............
}
}
然后在控制器中:
$scope.submit = function(effect) {
effectService.save(effect, function(response) {
console.log(response);
if(response.status == 400) {
$scope.errors = response.data.errors;
}
else {
$scope.errors = null;
}
...........
});
}
永远不要暗自相信你从客户那里得到的任何东西。
这很重要,所以我要重复一遍...
永远不要暗自相信你从客户那里得到的任何东西。
好吧,既然这样了...这个问题的答案真的取决于你。问问自己:
Is it a bad thing if my server-side code operates on unvalidated form data?
如果它很糟糕,那么您的服务器端代码需要保护自己免受这种可能性的影响。也许它很少会发生,也许永远不会发生。如果是这样的话,请认为自己很幸运。但它 可以 发生,因此请编写代码来处理这种情况。
如果还不错,如果系统可以正常运行,并且如果有人提交未经验证的数据真的没有任何问题,那么您可能不需要验证它。
基本上,您将两个截然不同的问题混合在一起。一个是:
Should I bother with server-side validation as a general practice?
答案是响亮的是。另一个是:
In my particular case, is it really necessary?
这个问题的答案完全取决于您和 logic/needs/etc。您正在构建的系统。肯定会有"validation"完全是为了提升用户体验的情况,不做就真的"go wrong"不行。 (例如,用户提交一个空表单可能只是创建一个空记录。结构上有效,只是没用。用户现在成功地拥有一个空记录,没什么大不了的。)是否属于这种情况取决于您。
例如,我有这样的HTML:
<form novalidate>
<input type="text" name="name" required />
<button type="submit" ng-submit="saveThis()"> Save </button>
</form>
我正在使用 Angular 处理错误 required
。
现在在服务器 post 方法上:
router.post('/', function(req, res, next) {
req.checkBody('name', 'name is required.').notEmpty();
var errors = req.validationErrors();
if(errors) {
//Send these errors to angular
} else {
//Code for saving data
}
}
我的问题是:
我已经在处理 Angular 中的错误。那么服务器端验证的必要性是什么?
如果你说如果Javascript在浏览器中被禁用,那么我需要服务器端错误。但是我的表格将永远不会被 posted 因为我使用 ng-submit
从 Angular 到 post 数据因为 Angular 如果 [=66] 也将不起作用=] 已禁用。
好吧,你可能会说,如果我的服务器从另一个应用程序收到请求,那么我认为这是必要的。如果您知道任何其他例外情况,请告诉我。但让我们回到我的应用程序范围:
如果需要将服务器错误发送到我的 Angular 应用程序,那么你能帮我将错误消息从服务器发送到 Angular 然后在 [=57= 上显示吗? ] 页?
更新:@paqash
请求的代码在我的控制器中:
$scope.submit = function(effect) {
effectService.save(effect, function(err, result, status) {
$scope.effect = null;
if(!(effect == null || effect == undefined)) {
if(!(effect._id == undefined || effect._id == '')) {
$scope.currentEffectName = null;
$location.path('/effect');
}
}
$setPristine();
$setUntouched();
});
}
在我的服务中:
function save(effect, callback) {
if(effect == undefined || effect._id == undefined || effect._id == '') {
$http.post('/api/effect', effect);
} else {
$http.put('/api/effect/' + effect._id, effect);
}
}
更新2:
这是我的 HTML:
<div class="col-md-10">
<ul class="errors" ng-if="errors.length > 0">
<li class="alert alert-danger" ng-repeat="error in errors"> {{error.msg}} </li>
</ul>
</div>
控制器保存功能如下:
$scope.submit = function(effect) {
effectService.save(effect, function(err, result, status) {
if(err) {
$scope.errors = err;
}
else {
$scope.errors = null;
}
...........
});
}
这是服务:
function save(effect, callback) {
if(effect == undefined || effect._id == undefined || effect._id == '') {
$http.post('/api/effect', effect).then(saveSuccessCallback, saveFailureCallback);
} else {
$http.put('/api/effect/' + effect._id, effect).then(saveSuccessCallback, saveFailureCallback);
}
}
function saveSuccessCallback() {
//Clear errors
return true;
}
function saveFailureCallback() {
//Raise errors
return false;
}
我删除了所有用于检查服务器端验证的客户端验证。现在,当我在没有在输入框中输入任何内容的情况下单击“保存”按钮时,我没有看到任何错误。
需要服务器端验证,是的。你永远不能相信客户。
您可以通过以下方式发送这些错误:
res.status(400).send({errors: errors});
然后在您的 saveThis
方法中检查响应的 http 代码,如果有错误,则显示它们。
编辑:您的 $http 方法可以与成功和错误回调链接在一起,如下所示:
$http.post('/someUrl', data, config).then(successCallback, errorCallback);
添加 errorCallback 方法后,当您的状态不是 200-something 时它会触发,您可以在那里检查响应对象的状态和错误数据。
更多信息here。
@vishal 更新(操作):
这是我从服务传递响应对象的方式:
function save(effect, callback) {
if(effect == undefined || effect._id == undefined || effect._id == '') {
$http.post('/api/effect', effect)
.then(function (response) {
//Clear errors
callback(response);
}, function(response) {
//Raise errors
callback(response);
});
} else {
............
}
}
然后在控制器中:
$scope.submit = function(effect) {
effectService.save(effect, function(response) {
console.log(response);
if(response.status == 400) {
$scope.errors = response.data.errors;
}
else {
$scope.errors = null;
}
...........
});
}
永远不要暗自相信你从客户那里得到的任何东西。
这很重要,所以我要重复一遍...
永远不要暗自相信你从客户那里得到的任何东西。
好吧,既然这样了...这个问题的答案真的取决于你。问问自己:
Is it a bad thing if my server-side code operates on unvalidated form data?
如果它很糟糕,那么您的服务器端代码需要保护自己免受这种可能性的影响。也许它很少会发生,也许永远不会发生。如果是这样的话,请认为自己很幸运。但它 可以 发生,因此请编写代码来处理这种情况。
如果还不错,如果系统可以正常运行,并且如果有人提交未经验证的数据真的没有任何问题,那么您可能不需要验证它。
基本上,您将两个截然不同的问题混合在一起。一个是:
Should I bother with server-side validation as a general practice?
答案是响亮的是。另一个是:
In my particular case, is it really necessary?
这个问题的答案完全取决于您和 logic/needs/etc。您正在构建的系统。肯定会有"validation"完全是为了提升用户体验的情况,不做就真的"go wrong"不行。 (例如,用户提交一个空表单可能只是创建一个空记录。结构上有效,只是没用。用户现在成功地拥有一个空记录,没什么大不了的。)是否属于这种情况取决于您。