5

Im building a frontend using Angular4. If a user submits a faulty form, an error message should be displayed.

At the moment, my error handling looks like this:

// This code is run when a response from the server is received
if ('error' in server_response) {
    for (let key in server_response.error {
      this.form.controls[key].setErrors({'error': true});
      let error_message_from_server = server_response.error[key];
    }
}

How I display the error in HTML:

<span class="error" *ngIf="field.invalid">Error: {{field.errors.error}} <br></span>

At the moment the field.invalid becomes true when a error is received, but the field.errors.error in an empty string.

Question: How can I set the field.errors.error message? This message should be the string in error_message_from_server

Please note that solving this by the use of if-statements in the HTML code is not an option. The amount of potential errors are in the hundreds.

2
  • Do you want to display the message after the server response or before the form submit ?and do you need something to be displayed near text box or any alert is fine ? Commented Aug 10, 2017 at 13:46
  • Sorry for not being clear. I have updated my question! The error should be displayed after the server response. The error message is received from the server. Commented Aug 10, 2017 at 13:49

2 Answers 2

3

this.form.controls[key].setErrors({'error': true}); // key is error and value is boolean not a string

try to set a string type value

for (let key in server_response.error {
  this.form.controls[key].setErrors({'error': erver_response.error[key]});
//Calling `setErrors` will also update the validity of the parent control.
}
Sign up to request clarification or add additional context in comments.

Comments

2

For the error string to be available on html level, you have to declare it on your component level:

error_message_from_server: string;

Then, on your response from server, you set the message to that string:

// This code is run when a response from the server is received
if ('error' in server_response) {
    for (let key in server_response.error {
      this.form.controls[key].setErrors({'error': true});
      error_message_from_server = server_response.error[key];
    }
}

Then you bind the string into your element:

<span class="error" *ngIf="field.invalid">Error: {{error_message_from_server}} <br></span>

3 Comments

I have 100+ input fields, all of them can receive a potential error message from the server. Creating 100+ public strings sounds like a really bad solution, especially when Angular already provides a 'field.errors.error'. I need to investigate if this is the only solution. Thanks for your input.
No problem, I didn't know you have 100+ inputs.
@Vingtoft did you ever find a way to pass the error message from the server to the control without creating a bunch of public strings?

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.