[Question] Validation of multiple fields within one request
Is it possible to validate group of fields in by submitting the whole form instead of one by one validation? E.g. I submit form with values {name, title, description}, server responds with
{
errors: {
name: "can't be empty",
title: ...
description: ...
}
}
And show this errors in associated form fields.
Thanks
Use onbeforesave handler for that http://vitalets.github.io/angular-xeditable/#editable-form
@eugef I am having trouble getting an error message to be displayed if I do in fact have a server error. The form will not close and the model is not updated, but I don't see the error message being displayed. Is this behavior correct? Shouldn't the error message be displayed somewhere like when the onbeforesave directive is used on individual fields?
Is this still an issue?
@beedaan can you provide a plnkr or jsfiddle showing your issue?
I can't replicate this issue anymore, so I'd like to close it.
@eugef please close this issue.
I guess it is the same issue I am seeing. http://jsfiddle.net/dongyb/msjc56w3/ The jsfiddle code posted above is forked from official example of Editable row. What I changed are:
- change the mocked post /saveUser to return 406.
- change the saveUser function of controller to reject with error message. After the changes, when edit a row and submit it, it fails but no error message shown
The documentation for onbeforesave says a string must be returned and that string is displayed as the error.
Thanks for your reply @ckosloski . In the section 'Validate remote' of documentation, it says " You can perform remote validation e.g. checking username in db. For that define validation method returning $q promise. If promise resolves to string validation failed. " In my example, http://jsfiddle.net/dongyb/msjc56w3/, it does return a promise that resolves to string.
Your issue is different that the one mentioned originally. It seems there is no handling/display of error messages for the onbeforesave that is set at the form level.
It does seem like the documentation does back that up:
If you need to send data on server before writing to local model then you should define form's onbeforesave. The result of form's onbeforesave is important for next step: true or undefined: local model will be updated and form will call onaftersave false: local model will not be updated and form will just close (e.g. you update local model yourself) string: local model will not be updated and form will not close (e.g. server error)
VS what is says for onbeforesave for a specific editable element:
If you need validation of individual editable elements then you should define onbeforesave on particular editable element. The result of child's onbeforesave is important for next step: string: submit will be cancelled, form will stay opened, string will be shown as error message not string: submit will be continued
@ckosloski you are right. As what you say my real question is that "no display of error message for the onfbeforesave that is set at the form level". It seems likely it is not designed to display error message at form (row in my case) level. It is only designed to display error message at field (td) level. I am working around it to call 'form.$setError(null, " ")' in the form's onbeforesave, which would show all fields in red border. It is not perfect but at least it tells something is wrong.
@dongyb80 I did take try some stuff to see if I could get errors to display on the form level, but I didn't get anything to work. If you come up with something, please post your code here.
@ckosloski Please see http://jsfiddle.net/dongyb/msjc56w3/4/ It shows what I said in previous post. It does NOT show error message. It shows all fields in red border to indicate something is wrong. To see how it behaves, edit a row and click save button. It is absolutely NOT good solution. Please let me know if you have better one :)
The key pieces of code posted below
$scope.saveUser = function(form, data, id) { //$scope.user not updated yet console.log("saving user"); angular.extend(data, {id: id}); let d=$q.defer(); $http.post('/saveUser', data).then( ()=>{d.resolve()}, (err)=>{ console.log("server error"); console.log(err); form.$setError(null, ' '); //please note that the second parameter is a blank char. d.reject("server error"); }) return d.promise; };
Thanks @dongyb80, at least it's a step in the right direction :)