Jamie Jamie - 1 year ago 170
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) {
title: "Fout",
text: message.error,
type: "error",
timer: 2000,
showConfirmButton: false

Answer Source

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

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download