vue-mc icon indicating copy to clipboard operation
vue-mc copied to clipboard

Error handle

Open ipetrov87 opened this issue 6 years ago • 1 comments

I am using Vue-mc with a installation of Laravel Framework 5.8.35. In this state of the project that I am working on, I do not want to use frontend validation. I want to leave the request to hit the server and serve will validate the request and return the validation errors if there are any.

All sounds good for now. But currently I am facing two problems:

  • I am not able to get the validation errors, returned from the server, the way that is described in the documentation
  • When I make a new request to save the model, old validation errors are not cleared. I also can`t find in documentation how I should clear them.

Getting validation errors

In doc it is written that validation errors should be get like this:

<span v-for="(error in task.errors.name)"></span>

and Errors array should looks like this

model.errors; //  {secret: [
              //     'Must be a string',
              //     'Must have a length between 2 and 8',
              //     'Can only use letters'
              //  ]}

But I get this:

errors: {
    "message": [
        "The given data was invalid."
    ],
    "errors": [
        {
            "name": [
                "The name field is required."
            ],
            "egn": [
                "The egn must be an integer.",
                "The egn must be 10 digits."
            ]
        }
    ]
}

What I should done to get the normal parsing of Laravel Request validation errors?

Remove old errors

When try to save the model and Laravel response with error validation messages they looks normal.

{
    "message": [
        "The given data was invalid."
    ],
    "errors": [
        {
            "name": [
                "The name field is required."
            ],
            "egn": [
                "The egn must be an integer.",
                "The egn must be 10 digits."
            ]
        }
    ]
}

Then I fill in the name and try again. Now also it looks ok:

{
    "message": [
        "The given data was invalid."
    ],
    "errors": [
        {
            "egn": [
                "The egn must be an integer.",
                "The egn must be 10 digits."
            ]
        }
    ]
}

Then I fix the the rest two errors and now I should see only my custom error. But this is not true. The two errors that i fix are still there and my custom error response is just added to errors:

{
    "message": [
        "The given data was invalid."
    ],
    "errors": [
        {
            "egn": [
                "The egn must be an integer.",
                "The egn must be 10 digits."
            ]
        }
    ],
    "customErrorData": [
        {
            ...
        },
        {
            ...
        },
        {
            ...
        }
    ]
}

I know that I should handle custom error by myself, but why the old validation error that I fixed are still there? Now for me it will great if there is something like model.errors.clear() or model.errors.reset() that will clear/reset model.errors.

ipetrov87 avatar Sep 17 '19 08:09 ipetrov87

There is a method clearErrors() on both Model and Collection.

You would need to create your own Response that accesses the errors correctly. It would be a good contribution to add a LaravelResponse that others can use. You can override createRequest in Model or Collection to produce a request which produces a response.

See:

  • https://github.com/FiguredLimited/vue-mc/blob/master/src/HTTP/Response.js#L22
  • https://github.com/FiguredLimited/vue-mc/blob/master/src/HTTP/Request.js#L14

rtheunissen avatar Oct 08 '19 18:10 rtheunissen