0

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

Tommy
  • 23
  • 4

1 Answers1

1

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

haakym
  • 12,050
  • 12
  • 70
  • 98
  • Thanks for answering. I'm using laravel validation too, but laravel validation don't get validation onclick. Parsley working like ajax stuff, then u writing some stuff in input, u get error message without submiting a form. I think u uderstand what i means. Sorry for my bad english :) – Tommy Mar 09 '16 at 10:00
  • Okay I see. If you use https://github.com/proengsoft/laravel-jsvalidation#laravel-5-javascript-validation it will give you live validation as you mentioned and reuses all the validation rules from Laravel. No worries I understand fine, best way is to keep practising. Happy coding! – haakym Mar 09 '16 at 10:17
  • Similar question: http://stackoverflow.com/questions/24540438/parsley-js-and-laravel-form-validation-errors – haakym Mar 09 '16 at 10:19