3

Hi I am using laravel code to check the email is unique or not and at the front end i am using jquery as follows:

blade.php page
user_email: {
                                                    required: true,
                                                    email: true,
                                                    remote: {
                                                        url: base_url + "/validate_email",
                                                        type: "post"
                                                    }
                                                },

and on post i used the following method validation_email in controller:

function validate_email(Request $request) {
        if ($request->input('user_email') !== '') {
            if ($request->input('user_email')) {
                $rule = array('user_email' => 'Required|email|unique:users');
                $validator = Validator::make($request->all(), $rule);
            }
            if (!$validator->fails()) {
                die('true');
            }
        }
        die('false');
    }

But when I fill the email and it validate it shows an error as CsrftokenMismatch Exception. When i disable the csrf token then csrf token then code is working otherwise it throws an exception. Please suggest me some solution for this.. Thank You

2 Answers 2

4

Pass the token along in your Request in your blade.

remote: {
    url: base_url + "/validate_email",
    type: "post"
    data: {
      _token: function() {
        return "{{csrf_token()}}"
      }
    }
  }
Sign up to request clarification or add additional context in comments.

1 Comment

hi @SteD where should i pass it.. I am using jquery validation and using it on single id.
0

its easy you CSRF token is in a hide field in your form, i dont remember de id but you can get that value and send it with the email in your post request for example:

required: true,
                                                    email: true,
                                                    remote: {
                                                        url: base_url + "/validate_email",
**YOU CAN ADD THE PARAMS HERE ( _id=" +$("#_id").val() +"),**
                                                        type: "post"
                                                    }

bassically you have add the token who is in your form to your post request, that value is hidden in the form, i hope i explain myself well

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.