Laravel Spark - no AJAX fields validation errors on register

942 views Asked by At

I just installed a Laravel Spark v4 - ran spark new project-name and followed the steps without any errors, the issue is that register form does not show any validation errors, not inline red warnings when input is not filled in or validation after the form is sent, what can be the issue? have not changed the generic register blade files.

When nothing is entered I get response code:

app.js:6175 POST http://127.0.0.1:8000/register 422 (Unprocessable Entity)

And the correct JSON response:

{"message":"The given data was invalid.","errors":{"name":["The name field is required."],"email":["The email field is required."],"password":["The password field is required."],"terms":["The terms must be accepted."]}}

Laravel's basic validation works on register form, but it does not work with AJAX request on login / register forms. Forms itself works when registering new users - but no feedback to the user.

3

There are 3 answers

2
Paul Grimes On BEST ANSWER

I believe this has to do with Laravel's "Consistent Exception Handling" under the hood. The VueJS application for Spark isn't yet taking this into consideration.

To fix locally, around line 54 of /spark/resources/assets/js/forms/errors.js, in the this.set function, change:

if (typeof errors === 'object') {
        this.errors = errors;
    } else {...

to

if (errors.errors && (typeof errors.errors === 'object')){
        this.errors = errors.errors;
    } else {...

Recompile your JS, and it should begin working again.

1
Bernd Da On

I got the same problem right now after a new installation. I changed the code according to Paul's answer but it still doesn't work. Maybe the reason is that i don't know how to recompile the JS...

I did: rerun npm run dev, run php artisan view:clear, run php artisan cache:clear but this all didn't help. Sorry I'm totally new to laravel

0
Debiprasad On

I believe you use Laravel 5.5 (based on Paul Grimes's answer about Consistent Exception Handling). For Laravel 5.5, you should be using Laravel Spark 5.0 instead of Laravel Spark 4.0. I believe Taylor Otwell must have fixed this issue.

My application was on Laravel 5.4 and Laravel Spark 4.0. I needed to upgrade to Laravel 5.5, but I am unable to upgrade Laravel Spark 5.0. So, the solution by Paul Grimes helped me. However, as some of the code in Laravel Spark 4.0 does not follow Consistent Exception Handling, I needed to modify my code as the following:

this.set = function (errors) {
    if (typeof errors.errors === 'object') {
        this.errors = errors.errors;
    } else if (typeof errors === 'object') {
        this.errors = errors;
    } else {
        this.errors = {'form': ['Something went wrong. Please try again or contact customer support.']};
    }
};