Jamie
Jamie

Reputation: 10886

Laravel and vue.js validation

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 
  });
}

Upvotes: 0

Views: 304

Answers (1)

Ohgodwhy
Ohgodwhy

Reputation: 50787

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

Upvotes: 1

Related Questions