0

I am using Angular2 and Java for standalone back-end in my web project.

I have validation handling in back-end. If I press, for example - save button, http call occurs to back-end (where my data is validated), and returns response as json which contains validation error metadata (or empty).

Now I want to show those errors to my angular2 view (for email and name). How can I do that?

        @Component({
selector: 'my-selector',
templateUrl: `
 <label for="name">Name</label>
 <input  id="name" [(ngModel)]="name" />
  <div>space for error message</div>
  <label for="email">Email</label>
   <input  id="email"  [(ngModel)]="email" />
 <div >space for error message</div>
  <button (click)="onSave()"> Save </button>`  })

      export class EmailForm { 
        private name:string;
        private email:string;
        private errorMessages
    public onSave() {
        return this._deService.add(.....);
    }}
  //and I have validationErrorMessages for that input,recived from back-end,let already deserialized form
     let messages: ErrorMeta[] = [
     { validatorKey:'required', message:'Required',controlId:'name'},
     { validatorKey: 'invalidEmailAddress',message:  'Invalid email address',controlId:'email'}
     ];
5
  • See angular.io/docs/ts/latest/guide/forms.html, angular.io/docs/ts/latest/cookbook/form-validation.html. You can use async validators to call to the server. Commented Sep 14, 2016 at 12:23
  • Dear Gunter,in my project I dont use forms. I recivie errors (http call already implemented and i recive errors ) as json and must show on view. So,I have @ messages and want to show those messages on screen.And set invaid state.? Maybe it is not possible,and I must use forms. But I have on question about using FormBuilder. This would work fine but what if our form grows with more controls? Commented Sep 14, 2016 at 14:26
  • Sure you can do it but this way the question is just too broad. Your question should show the code that demonstrates what you have tried and where you failed. How does the data look like that you get back from the server. How should we know how you actually want them shown. Commented Sep 14, 2016 at 14:29
  • I edited the question,please review it.And one question about using FormBuilder. This would work fine but what if our form grows with more controls? Commented Sep 14, 2016 at 16:32
  • Formbuilder should work fine with larger forms. Commented Sep 14, 2016 at 16:48

1 Answer 1

1

update

getErrors(controlId) {
  return this.messages.filter(err => err.controlId == controlId);
}
<label for="name">Name</label>
<input  id="name" [(ngModel)]="name" [class.invalid]="getErrors('name').length"/>

<div *ngFor="let err in getErrors('name')">
  {{err.validatorKey}} - {{err.message}}
</div>

<div>space for error message</div>
<label for="email">Email</label>

<div *ngFor="let err in getErrors('email')">
  {{err.validatorKey}} - {{err.message}}
</div>

<input  id="email"  [(ngModel)]="email" />
<div >space for error message</div>

<div *ngFor="let err in getErrors('email')">
  {{err.validatorKey}} - {{err.message}}
</div>

<button (click)="onSave()"> Save </button>
Sign up to request clarification or add additional context in comments.

10 Comments

No,each message must be under its control. And I think that,it would be better,set invalid state of something,maybe to Controls?
Ok,and how set invalid state for controls?
I updated my answer (only for the first input, same for the others)
Nice,can I resolve that in back-end,not touching to control?
I don't understand what you mean
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.