0

From my Laravel api I receive the following validation errors within json:

{
  "error": {
    "billabletime": [
      "The billabletime field is required."
    ],

   "time": [
      "time bust be a integer."
    ]
  }
}

So how would I show them in vue.js? Right now I've this but that's obviously not working:

 showError (message) {
  swal({
    title: "Fout", 
    text: message.error, 
    type: "error", 
    timer: 2000,
    showConfirmButton: false 
  });
}
2
  • You first should use message.error.billabletime in text object Commented Sep 23, 2016 at 5:11
  • Yes but billabletime could also be some other error message. (made an edit) Commented Sep 23, 2016 at 5:14

1 Answer 1

1

Like this:

var errorString = '';
if (message.hasOwnProperty('error')) {
    for(var prop in message.error) {
        if (Array.isArray(prop)) {
            for (var msg in prop) {
                errorString += prop[msg] . '<br/>';
            } 
        } else {
            errorString += message.error[prop] . '<br/>';
        }
    }
}

Something simple like this should give you the desired result. Not necessary to know index names.

Edit added functionality to handle stirng/array

Sign up to request clarification or add additional context in comments.

Comments

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.