phplaravelparsley

Parsley and Laravel username check


I'm using Laravel 5.1 and parsley.js, what i want todo: Display error with parsley if username already exist.

My laravel controller code to check response:

public function checkUsername($username)
{
    $user = User::whereUsername($username)->first();

    if ($user) {
        return Response::make('1'); // Username EXISTS
    }
    return Response::make('0'); // Username free, can register!
}

Any ideas how to do that with parsley.js? Thanks for helping guys!

P.S: I got some route to check this response: ModuleRoute::get('auth/username/check/{username}', 'RegistrationController@checkUsername');


Solution

  • This doesn't address your question 100%, but I hope it helps!

    I completely understand if you're running this code out to just get to grips with how the framework works. However, it may be better practice to utilise Laravel's validation features. In this case you could use the validator's exists rule to check if a username exists before submitting a form.

    This link shows you how to get started with using the Laravel's validation features: https://laravel.com/docs/5.2/validation#validation-quickstart

    And here's the rule that does what you're doing (and more): https://laravel.com/docs/5.2/validation#rule-exists

    If you're interested in utilising a js framework that will assist you on doing form validation please check out this great package: https://github.com/proengsoft/laravel-jsvalidation#laravel-5-javascript-validation