TokenMismatchException in VerifyCsrfToken.php line 68,when i try to post a form.
<form method="post" action="url('add')" >
{{ csrf_field() }}
<input type="text" name="title" />
<input type="submit" value="Add" />
</form>
Its was working fine before i added this package.
Instead of {{ Form::open() }} either you can manually add hidden input field in the form just like Andranik Petrosyan suggested
But still I would like you to try
{!! csrf_field() !!} instead of {{ csrf_field() }}
If you don't use Laravel Form, for example:
{!! Form::open([]) !!}
{!! Form::close() !!}
you can use:
<input type="hidden" name="_token" value="{{ csrf_token() }}">
Sometimes you need to clear your cache as well.
Related
EDIT I am not using resource controller but I believe my route is correct
I have a form on it called recordings I have the form like:
<form class="form-horizontal" role="form" method="POST" action="{{ url('/recordings/create') }}" enctype="multipart/form-data">
<input type="hidden" name="_token" value="{{ csrf_token() }}">
<div class="form-group">
<label class="col-md-4 control-label">Client Name</label>
<div class="col-md-6">
{!! Form::select('ClientName', $client_options, '', array('class' => 'form-control')) !!}
</div>
</div>
<div class="form-group">
<label class="col-md-4 control-label">File</label>
<div class="col-md-6">
<input type="file" class="form-control" name="FileUpload">
</div>
</div>
<div class="form-group">
<div class="col-md-6 col-md-offset-4">
<button type="submit" class="btn btn-primary">
Submit
</button>
</div>
</div>
</form>
Then in my RecordingsController
public function store()
{
var_dump(Input::file('FileUpload'));
var_dump(Input::get('ClientName')) ;
}
My route:
Route::get('recordings/create', 'RecordingsController#create');
Route::post('recordings/create', 'RecordingsController#store');
Why is it the var_dump is null? I have a dropdown which has values in it and I already selected one. The other one is file input filed which I also selected already a file.
Try:
public function store(Request $request) {
$ClientName = $request->ClientName:
if ($this->request->hasFile('FileUpload'))
{
$files = $this->request->file('FileUpload');
....
Or simple use
dd($request);
Always a good idea here is to use Firebug - to check out which values are submitted to your script.
Works now. Problem is I am sending huge data in my post. So I did is changed the post_max_size in my php.ini. Weird though I am not getting error regarding that.
i have a bootstrap modal dialog which use laravel form to register a user.
Here's the code:
<div id="addPenggunaModal" class="modal hide fade" tabindex="-1" role="dialog" aria-labelledby="ModalLabel" aria-hidden="true">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">Ă—</button>
<h3 id="ModalLabel">Tambah Pengguna Baru</h3>
</div>
<div class="modal-body">
{{ Form::open(array('url'=>'users/addpengguna','class'=>'form-horizontal', 'method'=> 'POST')) }}
<ul>
#foreach($errors->all() as $error)
<li>{{ $error }}</li>
#endforeach
</ul>
<div class="control-group">
<label for="firstname" class="control-label">First Name:</label>
<div class="controls">
{{ Form::text('firstname', null, array('class'=>'span3', 'placeholder'=>'First Name')) }}
</div>
</div> <!-- /field -->
<div class="control-group">
<label for="lastname" class="control-label">Last Name: </label>
<div class="controls">
{{ Form::text('lastname', null, array('class'=>'span3', 'placeholder'=>'Last Name')) }}
</div>
</div> <!-- /field -->
<div class="control-group">
<label for="email" class="control-label">Email Address: </label>
<div class="controls">
{{ Form::text('email', null, array('class'=>'span3', 'placeholder'=>'Email Address')) }}
</div>
</div> <!-- /field -->
<div class="control-group">
<label for="password" class="control-label">Password:</label>
<div class="controls">
{{ Form::password('password', array('class'=>'span3', 'placeholder'=>'Password')) }}
</div>
</div> <!-- /field -->
<div class="control-group">
<label for="confirm_password" class="control-label">Confirm Password:</label>
<div class="controls">
{{ Form::password('password_confirmation', array('class'=>'span3', 'placeholder'=>'Confirm Password')) }}
</div>
</div> <!-- /field -->
<div class="control-group">
<label for="type_user" class="control-label">Tipe Pengguna:</label>
<div class="controls">
{{ Form::radio('level', '1'); }} Supervisor
{{ Form::radio('level', '0'); }} Sales
</div>
</div> <!-- /field -->
</form>
</div>
<div class="modal-footer">
{{ Form::submit('Simpan', array('class'=>'button btn btn-primary','id'=>'mdl_save_change'))}}
<button class="btn" data-dismiss="modal" aria-hidden="true">Batal</button>
</div>
{{ Form::close() }}
</div>
then i use the controller to save the details:
public function postAddpengguna(){
/* function to add user in data pengguna */
$validator = Validator::make(Input::all(), User::$rules);
if($validator -> passes()){
$user = new User;
$user->firstname = Input::get('firstname');
$user->lastname = Input::get('lastname');
$user->email = Input::get('email');
$user->password = Hash::make(Input::get('password'));
$user->level = Input::get('level');
/* save the following details */
$user->save();
return Redirect::to('pengguna');
} else {
return Redirect::to('index');
}
}
but the form doesn't save any data to database. I have another page called registration and it works.
my questions:
how to trace POST from laravel form submit, is there any browser extension?
how to trace error log in laravel
any ideas what's going on in my problem?
thank you in advance.
UPDATE
Here's the screenshot that describe how this works.
bootstrap modal:
when i press the submit button (blue button in modal) i want it to save the data to db. The function php is shown above.
PS. i don't use any AJAX to call the value from the FORM. But when i use the AJAX, it always error because TOKEN is missing.
First of all check the action and _token field of form . To add token field in your form you should include the following line in your form:
<input type="hidden" name="_token" value="{{csrf_token()}}">
To re-use bootstrap modal in your project you can check this Github link
In the latest versions of laravel 5 you can use a shortcut to get the token field.
<form ... >
{!! csrf_field() !!}
</form>
In this case you'll get something like
<input type="hidden" name="_token" value="hpyL7cUbCMFBGRfCi2dpzE5XHGj8WuyY2jqloKRx">
You can in any case get the token string calling csrf_token(), anyway I honestly prefer the csrf_field() alternative.
You may use this code with your ajax code:
$(function() {
$.ajaxSetup({
headers: {
'X-CSRF-TOKEN': {!! json_encode(csrf_token()) !!}
}
});
});
I updated Laravel 4.1 recently and an exiting modal window form is not working correctly. I have figured out that for some reason after the Laravel 4.1 update, Laravel is auto inserting an tag into my modal body. (I have the Form::close() in the footer. I was just curious if anyone else has seen this or can offer an explanation of why this is happening and how to prevent it. I am good about searching through my issues, but this one is not yielding me any results.
Note that if I move my submit button into the modal-body div, then submit works as expected and updates goes through the normal process.. but for some reason with this particular modal, having the submit button in the footer puts the submit button outside of the form as the form close is auto inserted before the end of the modal-body div. Also strangely enough I have this working on another page, and everything works as expected.
Here is the relevant code:
<div class="modal-body">
<?php
$access = Session::get('user_access');
$userid = Session::get('user_id');
?>
{{ Form::open(array('method'=>'POST','route' => 'users.store', 'style' => 'display:inline')) }}
#foreach($user as $userinfo)
<!-- Set hidden form element with userid embedded -->
<input type="hidden" name='id' id='id' value={{ $userid }}>
<!-- Display the username and profile Picture -->
<h2><center>{{ $userinfo->username }}</center></h2>
<br><br>
<!-- 2 Column Form to change user information and display current status -->
<div class ='container col-md-offset-1'>
<div class='row col-md-3'>
<div>
{{ Form::label('givenname', 'First Name:') }} <br>
<input type="text" name='givenname' id='givenname' value={{ $userinfo->givenname }}>
</div><br />
<div>
{{ Form::label('surname', 'Last Name:') }} <br>
<input type="text" name='surname' id='surname' value={{ $userinfo->surname }}>
</div><br />
<div>
{{ Form::label('email', 'Email Address:') }} <br>
<input type="text" name='email' id='email' value={{ $userinfo->email }}>
</div><br />
</div>
<div class='row col-md-3'>
<div>
{{ Form::label('password', 'New Password:') }} <br>
<input type="password" name='password' id='password' value={{ $userinfo->password }}>
</div><br />
<div>
{{ Form::label('password_confirmation', 'Confirm New Password:') }} <br>
<input type="password" name='password_confirmation' id='password_confirmation' value={{ $userinfo->password }}>
</div><br />
<div>
{{ Form::label('useraccess', 'Current Subscription Status:') }} <br>
{{ $access }}
</div><br />
</div>
</div>
#endforeach
</div> <!-- End Modal Body -->
<div class="modal-footer">
{{ Form::submit('Save', array('class' => ' btn btn-warning')) }}
<!-- Close the form -->
{{ Form::close() }}
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
</div>
When I run the developer tools in the browser, I can see the auto insert
<form method="POST" action="http://dev.app.myapp.com/users" accept-charset="UTF-8"><input name="_token" type="hidden" value="<TOKEN">
<!-- Set hidden form element with userid embedded -->
<input type="hidden" name="id" id="id" value="6">
<!-- Display the username and profile Picture -->
<h2><center>johndoe123</center></h2>
<br><br>
<!-- 2 Column Form to change user information and display current status -->
<div class="container col-md-offset-1">
<div class="row col-md-3">
<div>
<label for="givenname">First Name:</label> <br>
<input type="text" name="givenname" id="givenname" value="John">
</div><br>
<div>
<label for="surname">Last Name:</label> <br>
<input type="text" name="surname" id="surname" value="Doe">
</div><br>
<div>
<label for="email">Email Address:</label> <br>
<input type="text" name="email" id="email" value="johndoe#notarealemail.com">
</div><br>
</div>
<div class="row col-md-3">
<div>
<label for="password">New Password:</label> <br>
<input type="password" name="password" id="password" value="Encrypted Password String">
</div><br>
<div>
<label for="password_confirmation">Confirm New Password:</label> <br>
<input type="password" name="password_confirmation" id="password_confirmation" value="Encrypted Password String">
</div><br>
<div>
<label for="useraccess">Current Subscription Status:</label> <br>
User </div><br>
</div>
</div>
</form>
<div class="modal-footer">
<input class=" btn btn-success" type="submit" value="Save">
<!-- Close the form -->
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
</div>
The reason is that your mark-up is lacking proper structure. You begin the <form> in some element, then close it inside another element.
Check out the page source (ALT+CMD+U on Mac). You will notice </form> exactly where you originally put Form::close() (inside <div class="modal-footer">).
That position, however, is invalid mark-up, as I mentioned before. The browser doesn't know exactly what you want, but it will try to come to a conclusion, so it correctly moves </form> down one level.
What you are seeing in developer tools is not what was printed out by Laravel, but actually how the browser interpreted your invalid mark-up.
A solution for this specific case: move Form::close() after <div class="modal-footer">.
Using Laravel 4's Radio form helper, I cannot use Bootstrap 3's radio button syntax and get the inputs to be checked by default.
Here is a normal radio button setup, that works properly
{{Form::radio('awesome_radio_input', 'yes', true)}} Yes
{{Form::radio('awesome_radio_input', 'no')}} No
And here is the BS3 syntax, where I am not getting any radio inputs checked by default
<div class="radio-inline">
<label>
{{Form::radio('silly_radio_input', 'yes', true)}}
Yes
</label>
</div>
<div class="radio-inline">
<label>
{{Form::radio('silly_radio_input', 'no')}}
No
</label>
</div>
Any ideas as to why the syntax would break the Radio helper?
Try this :
<div class="form-group">
{{ Form::label('gender', 'Gender') }}
<div class="form-inline">
<div class="radio">
{{ Form::radio('gender', 'true', true) }}
{{ Form::label('men', 'Men') }}
</div>
<div class="radio">
{{ Form::radio('gender', 'false') }}
{{ Form::label('woman', 'Woman') }}
</div>
</div>
</div>
when clicking from a template to this button :
<form action="{{ path('fos_user_registration_register') }}" method="post">
<button class="btn btn-small btn-success" type="submit">S'inscrire</button>
</form>
I then access my UserBundle/Resources/view/Registration/register/html.twig ( which then overrides the FOSUSerBundle default form ) :
{% extends "FOSUserBundle::layout.html.twig" %}
{% block fos_user_content %}
<form action="{{ path('fos_user_registration_register') }}" {{ form_enctype(form) }} method="POST" class="fos_user_registration_register">
{{ form_rest(form) }}
<div>
<input type="submit" value="{{ 'registration.submit'|trans({}, 'FOSUserBundle') }}" />
</div>
</form>
{% endblock fos_user_content %}
For some reasons, all validation messages appear (such as Please enter a username) although I did not event submit the form !?
Any explanations for that?
How can I customized the FOSUser template and redirect error_message, not making it appear or changing messages or display?
at the end i overrided the registration_content view as such:
<form action="/projet_etienne_auth/web/app_dev.php/register/" method="POST" class="fos_user_registration_register">
{{ form_errors(form) }}
{{ form_widget(form._token) }}
<div>
<label for="fos_user_registration_form_username" class="required">Nom d'utilisateur :</label>
<input type="text" id="fos_user_registration_form_username" name="fos_user_registration_form[username]" required="required" maxlength="255" pattern=".{2,}" /></div>
<div>
<label for="fos_user_registration_form_email" class="required">Adresse e-mail :</label>
<input type="email" id="fos_user_registration_form_email" name="fos_user_registration_form[email]" required="required" /></div>
<div>
<label for="fos_user_registration_form_plainPassword_first" class="required">Mot de passe :</label>
<input type="password" id="fos_user_registration_form_plainPassword_first" name="fos_user_registration_form[plainPassword][first]" required="required" />
</div>
<div><label for="fos_user_registration_form_plainPassword_second" class="required">VĂ©rification :</label>
<input type="password" id="fos_user_registration_form_plainPassword_second" name="fos_user_registration_form[plainPassword][second]" required="required" />
</div><div>
<input type="submit" value="Enregistrer" />