Search code examples
formstwitter-bootstraplaraveltokenmismatch

Laravel 5 - TokenMismatchException in VerifyCsrfToken.php


I have created an easy PUT form for my site (also I have GET and POST forms, which work fine).

<h1>Edit {!! $brands->brand_name !!}</h1>

    <!-- if there are creation errors, they will show here -->
    {!! Html::ul($errors->all()) !!}

    {!! Form::model($brands, array('route' => array('brands.update', $brands->id), 'method' => 'PUT', 'files' => true)) !!}

    <div class="form-group">
        {!! Form::label('brand_name', 'Name') !!}
        {!! Form::text('brand_name', null, array('class' => 'form-control')) !!}
    </div>

    <div class="form-group">
        {!! Form::label('brand_name_rus', 'Name_rus') !!}
        {!! Form::text('brand_name_rus', Input::old('brand_name_rus'), array('class' => 'form-control')) !!}
    </div>

    <div class="form-group">
        {!! Form::label('Your image') !!}
        <img src="http://parasha.dev/{{ $brands->img_brand_path }}"/>
    </div>

    <div class="form-group">
        {!! Form::label('img_brand_path', 'New image') !!}
        {!! Form::file('img_brand_path') !!}
    </div>

    {!! Form::submit('Edit the Brand!', array('class' => 'btn btn-primary')) !!}

    {!! Form::close() !!}

Also I'm using Clip Two Theme as a layout of my admin panel that include those forms. And when I use both (layout and my forms, it gives me an arror -> TokenMismatchException in VerifyCsrfToken.php). So this bootstrap theme somehow causes that error.

So I have two questions. First, how can I solve this problem. And the second, can I create laravel form without token autocreation? Sorry for my English)


Solution

  • It appears you are not including the csrf_token in your form.

    Add {!! csrf_field() !!} into your form. If you use ajax requests, you can place the {!! csrf_field() !!} in the 'master' template and reference it in the ajax header like so:

    $.ajaxSetup({
    headers: { 'X-CSRF-Token' : $('meta[name=_token]').attr('content')}
    });