mercredi 24 octobre 2018

Laravel Prevent Form from Re submitting

I have a form of Adding Album in database

 {!! Form::open(['method' => 'POST', 'route' => ['admin.album.store'], 'enctype' => 'multipart/form-data', 'id' => 'CreateAlbumForm']) !!}
<input type="hidden" name="_token" value="">

// other fields

{!! Form::submit(trans('global.app_save'), ['class' => 'btn btn-danger']) !!}
{!! Form::close() !!}

It is working perfectly.

What i need is i need to prevent user from clicking submit button multiple times. which i know is possible with jquery ( disabling submit button on click). But i want to make it using csrf protection(Server side) when user does not have javascript enabled.

I have tried following

Adding Below function in VerifyCsrfToken.php

protected function tokensMatch($request)
{
    $token = $request->input('_token') ?: $request->header('X-CSRF-TOKEN');

    if (!$token && $header = $request->header('X-XSRF-TOKEN')) {
        $token = $this->encrypter->decrypt($header);
    }

    $tokensMatch = ($request->session()->token() == $token) ? TRUE : FALSE;

    if($tokensMatch) $request->session()->regenerateToken();

    return $tokensMatch;
}

And adding _token inside $dontFlash array in file app\Http\Requests\FormRequest.php

protected $dontFlash = ['password', 'password_confirmation', '_token'];

It gives me Token Mismatch error when i click on submit button more than 2 times.

What i want is whether a user clicks on submit button multiple times or single time the record should be inserted only single time. OR It should throw custom error when clicking more than one time.



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire