Jamie Jamie - 2 months ago 32
JSON Question

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

Answer

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