Laravel 5, Forms, TokenMismatchException in VerifyCsrfToken.php line 46 - laravel

I keep getting the "TokenMismatchException in VerifyCsrfToken.php line 46" error when submitting forms.
This is one of the forms:
{!! Form::model($product, array('url' => 'product/'.$product->id, 'class' => 'form', 'method' => 'PATCH')) !!}
<div class="form-group">
{!! Form::textarea('note', $product->note,
array('class'=>'form-control', 'id'=>'product-note', 'placeholder'=>Lang::get('customtranslation.form_placeholder_note'), 'rows'=>3)) !!}
<br />
<span class="btn btn-link" id="remove-note" role="button"><i class="fa fa-times"></i> {{ Lang::get('customtranslation.button_txt_reset_note') }}</span>
</div>
<div class="form-group">
{!! Form::submit(Lang::get('customtranslation.button_txt_finish_edit_product'), array('class'=>'btn btn-success')) !!}
</div>
<div class="form-group">
<!-- Custom tags -->
{!! Form::label('additional-tags', Lang::get('customtranslation.form_edit_label_additional_tags')) !!}
{!! Form::text('additional-tags','', array('id'=>'additional-tags', 'data-role'=>'tagsinput')) !!}
</div>
{!! Form::close() !!}
The input element with name "_token" gets generated and set as expected.
The strange thing is that this occurs only in Internet Explorer (IE11). Chrome and FF make a submit without any problems.
Does anyone else have this problem and a possible solution?

Internet explorer rejects sessions from domains with an underscore it. This is a known issue.
Please see here: Issue with Session and Cookie in Internet Explorer for websites containing underscore
And also:
http://blogs.msdn.com/b/ieinternals/archive/2009/08/20/wininet-ie-cookie-internals-faq.aspx

Probably you have not set the _token parameter in your request to the server or you have put it in somewhere incorrect.

Related

How can I keep selected the previous value when updating a form?

I am using laravel collctive form. I want to update only the quantity field of the following form keeping the blood_id field readonly. When I submit the form I am not getting blood_id value.
How can i solve it?
{!! Form::model($bloodBank, ['route' => ['bloodBanks.update', $bloodBank->id], 'method' => 'put']) !!}
<div class="row">
<div class="col-lg-8">
<div class="form-group">
{!! Form::label('blood_id', 'Blood Group', ['class' => 'form-control-label']);!!}
{!! Form::select('blood_id', $bloods , null , ['placeholder' => 'Choose Blood Group',"class"=>"form-control",'disabled' => true]) !!}
</div>
</div>
</div>
<div class="row">
<div class="col-lg-8">
<div class="form-group">
{!! Form::label('quantity','Quantity', ['class' => 'form-control-label']);!!}
{!! Form::number("quantity",null, ["class"=>"form-control form-control-label",'min'=>'0']) !!}
<span class="validation-error">{{ $errors->first("quantity") }}</span>
</div>
</div><!-- col-12 -->
</div>
<button class="btn btn-info">Update </button>
{!! Form::close() !!}
For disabled fields, you may want to add hidden fields which won't display on the rendered page BUT will be included in the request object. E.g.
{{ Form::hidden('blood_id', $bloods) }}
This is in addition to the displayed field you already have which is disabled.

laravelcollective/html not finding any of my controllers

I am getting this error from a login page
Action App\Http\Controllers\Admin\LoginController#authenticate not defined. (View: /Applications/XAMPP/xamppfiles/htdocs/lectureme/resources/views/admin/login.blade.php)
I know for certain that this controller exists though, so why is it unable to find it? I have also created a new controller in the controller root and named it TestController, and tried routing to that instead, but that was also apparently not found.
Any suggestions for how to get access to the controller? Form code:
{!! Form::open(['action' => 'LoginController#authenticate']) !!}
<div class="form-group">
<div class="form-group">
{!! Form::label('username', 'Username:') !!}
{!! Form::text('username', null, ['class'=>'form-control']) !!}
</div>
<div class="form-group">
{!! Form::label('email', 'Email Address:') !!}
{!! Form::text('email', null, ['class'=>'form-control']) !!}
</div>
</div>
<div class="form-group">
{!! Form::submit('Login', ['class'=>'btn btn-primary']) !!}
</div>
{!! Form::close() !!}
I have also tried composer dump-autoload and php artisan cache:clear
Make sure you namespaced your controller properly. Assuming you have placed your controller in the App\Http\Controllers\Admin directory it would be:
namespace App\Http\Controllers\Admin

Laravel back()->withInput(); not working

I am currently following a video tutorial on a login form. Here's the code used in case Auth::attempt() fails:
return back()->withInput();
This should return the user to the form and fill out the inputs again (email, password). However the fields stay empty, while login is working correctly.
How can I fix this?
This is my form:
{!! Form::open(array('route' => 'handleLogin')) !!}
<div class="form-group has-feedback">
{!! Form::text('email', null, array('class' => 'form-control', 'placeholder' => 'EMail')) !!}
<span class="glyphicon glyphicon-envelope form-control-feedback"></span>
</div>
<div class="form-group has-feedback">
{!! Form::password('password', array('class' => 'form-control', 'placeholder' => 'Password')) !!}
<span class="glyphicon glyphicon-lock form-control-feedback"></span>
</div>
<div class="row">
<div class="col-xs-8">
<div class="checkbox icheck">
<label>
<input type="checkbox"> Remember Me
</label>
</div>
</div>
<!-- /.col -->
<div class="col-xs-4">
{!! Form::token() !!}
{!! Form::submit(null, array('class' => 'btn btn-primary btn-block btn-flat')) !!}
</div>
<!-- /.col -->
</div>
{!! Form::close() !!}
Edit: Found out that the following code I have works for login but the else statement is not executed when the login credentials are wrong. why?
public function handleLogin(Request $request)
{
$this->validate($request, User::$login_validation_rules);
$data = $request->only('email', 'password');
if(\Auth::attempt($data)){
return redirect()->intended('home');
}else {
return back()->withInput();
}
}
So, as we discussed, the problem is the web middleware. In previous revisions of the Laravel 5.2 framework, if you wanted to enable sessions and error variables, you had to wrap the routes in the web middleware.
As of version 5.2.27, this is not the case anymore, since that middleware group is already applied to all routes, by default.
Knowingly, having version 5.2.27 or later, and using the web middleware group, causes issues with those same variables, common issues are session variables not being passed around, and the $errors variable provided by the class \Illuminate\View\Middleware\ShareErrorsFromSession, that is returned by the Laravel validation API is set, but empty**.
Summary
If you have Laravel 5.2.27 and later, you don't need to wrap the routes in the web middleware group.
If the version is lower than that, then you are required to do so, in order to use session variables and get validation errors.
Sources
https://github.com/laravel/framework/issues/13313
https://laravel.com/docs/5.2/middleware#middleware-groups
Out of the box, Laravel comes with web and api middleware groups that contains common middleware you may want to apply to web UI and your API routes:
App/Http/Kernal.php
protected $middlewareGroups = [
'web' => [
\App\Http\Middleware\EncryptCookies::class,
\Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class,
\Illuminate\Session\Middleware\StartSession::class,
\Illuminate\View\Middleware\ShareErrorsFromSession::class,
\App\Http\Middleware\VerifyCsrfToken::class,
],
'api' => [
'throttle:60,1',
'auth:api',
],
];
Take a look at web which contains StartSession and ShareErrorsFromSession which are responsible for session handling so if you want to show validation errors or persist old data then you have to include them in your middleware group. in this case its web.
You Have To make the condiotion if The form has been back with errors, it should been fill out.
The {{old('nom')}} function look for The Previous Filled Value for a given field and echo it.
you can use it as below:
<div class="col-sm-8">
<input type="text"
class="form-control form-control-sm #error('nom') is-invalid #enderror"
id="nom"
name="nom"
placeholder="Ecriver le nom votre ecole "
value="{{old('nom')}}"
>
</div>
try changing
return back()->withInput();
with
return redirect()->back()->withInput(\Input::all()

Yield a form on a different page using laravel

I'm making a twitter look-a-like app.
I have a form to post a message on 1 blade (createMessage.blade.php) which is as followed:
#section('message')
{!! Form::open(['url' => 'message/postmessage']) !!}
<div class="form-group col-lg-6 col-lg-offset-3">
{!! Form::label('body') !!}
{!! Form::textarea('body', null,['class' => 'form-control']) !!}
</div>
<div class="form-group col-lg-6 col-lg-offset-3">
{!! Form::submit('Post message', ['class' => 'btn btn-primary form-control']) !!}
</div>
{!! Form::close() !!}
#endsection`
Now I want to get my form on different pages (such as my timeline) but if I try #yield('message') it doesn't work.
timeline:
#extends('layouts.app')
#section('content')
#yield('message')
//some code for my timeline
#endsection
I can't find out why it doens't work.
Thanks in advance!
You have to use
#include('createMessage.blade.php') in the timeline file and remove the #section declaration in createMessage.blade.php

Adding Route and Perameter in Form action in html in laravel 5

I am unable to get this, My Route is as under;
Route::patch('/info/{iplive}', ['as' => 'info', 'uses' => 'InfoController#index']);
and Here is My Form,
{!! Form::open(['url'=>'info/', 'id'=> 'lookup', 'class' => 'user_form','method'=>'PATCH']) !!}
<div class="row center-block">
<div class="col-lg-6 center-block">
<div class="input-group">
<input type="text" id="lookup" name="lookup" value="" class="form-control" placeholder="Search for Address">
<span class="input-group-btn">
<button class="btn btn-default" type="submit">Lookup</button>
</span>
</div>
</div>
</div>
<!--</form> -->
{!! Form::close() !!}
I am in efforts to use a url like, link but unable to get it done :(
your route expects a paramater to be filled in,
try something like this
{!! Form::open(['method' => 'PATCH', , 'id'=> 'lookup', 'class' => 'user_form', 'route' => [ 'info' , $your_iplive_variable]]) !!}
where $your_iplive_variable should be the paramater you wish to fill in.

Resources