In Laravel I'm getting a "csrf" issue "Illuminate \ Session \ TokenMismatchException"
Route::group(array('before'=>'guest'),function()
{
Route::get('/user/create',array('uses'=>'UserController#getCreate'));
Route::get('/user/login',array('uses'=>'UserController#getLogin','as'=>'postCreate'));
Route::group(array('before'=>'csrf'),function()
{
Route::get('/user/create','UserController#postCreate');
Route::get('/user/login','UserController#postLogin');
});
});
that is controller
class UserController extends BaseController{
public function getCreate()
{
//return View::make('hello');
return View::make('user.register');
}
that is view
<div class="container">
<h1>Register</h1>
<form role="form" method="post" action="{{ URL::route('postCreate')}}">
<div class="form-gourp">
<label for="username">Username: </label>
<input id="username" name="username" type="text" class="form-control" />
</div>
<div class="form-gourp">
<label for="password">Password: </label>
<input id="password" name="password" type="text" class="form-control" />
</div>
<div class="form-gourp">
<label for="username">confirm Password: </label>
<input id="cpassword" name="cpassword" type="text" class="form-control" />
</div>
{{form::token()}}
<div class="form-gourp">
<input type="submit" value="register" class="btn btn-default"/>
</div>
</form>
When I add {{form::token }}, it shows the error "Illuminate \ Session \ TokenMismatchException"
You are wrong in your routes.
Route::group(array('before'=>'csrf'),function()
{
Route::get('/user/create','UserController#postCreate');
Route::get('/user/login','UserController#postLogin');
});
Those should be post, like the following
Route::group(array('before'=>'csrf'),function()
{
Route::post('/user/create','UserController#postCreate');
Route::post('/user/login','UserController#postLogin');
});
Read more about CSRF on wiki and laravel doc.
Use {{ Form::open() }} and {{ Form::close() }} rather than the <form> tags
Related
Duplicate of https://github.com/laravel/framework/issues/31123
Laravel Version: 6.8.0 ,6.10.1
PHP Version: 7.4
Languages from https://github.com/caouecs/Laravel-lang
Description:
For some languages, when validated errors too much, errors not show in blade (not send in session)
Language example: ru (not show >=6 errors), si (not show >=8 errors) in my code.
Steps To Reproduce:
New laravel 6 app + https://github.com/caouecs/Laravel-lang
conf/app.php
locale' => 'ru',
// locale' => 'si',
View:
#extends('layouts.app')
#section('content')
<div class="container">
#if ($errors->any())
<div class="alert alert-danger">
<ul>
#foreach ($errors->all() as $error)
<li>{{ $error }}</li>
#endforeach
</ul>
</div>
#endif
<form class="form" method="post" action="{{route('testvalid')}}">
#csrf
<input class="form-control" name="field1" />
<input class="form-control" name="field2" />
<input class="form-control" name="field3" />
<input class="form-control" name="field4" />
<input class="form-control" name="field5" />
<input class="form-control" name="field6" />
<input class="form-control" name="field7" />
<input class="form-control" name="field8" />
<input class="form-control" name="field9" />
<input class="form-control" name="field10" />
<input class="form-control" name="field11" />
<input type="submit" class="btn btn-primary">
</form>
</div>
#endsection
Action:
public function testvalidpost(Request $request){
$request->validate([
'field1'=>['required'],
'field2'=>['required'],
'field3'=>['required'],
'field4'=>['required'],
'field5'=>['required'],
'field6'=>['required'],
'field7'=>['required'],
'field8'=>['required'],
'field9'=>['required'],
'field10'=>['required'],
'field11'=>['required'],
]);
echo ok;
}
Try to set SESSION_DRIVER=file to get it work
See related
Controllers/HomeController.php
public function edit(Task $task)
{
return view('edit', compact('task'));
}
public function update(Request $request, Task $task)
{
$request->validate(['title' => 'required|min:3', 'description' => 'required', ]);
$task->title = $request->title;
$task->description = $request->description;
$task->save();
$request->session()
->flash('message', 'Successfully modified the task!');
return redirect('viewalltask');
}
routes/web.php
Route::post('/{task}/', 'HomeController#update')->name('update');
views/edit.blade.php
<form action="{{url('', [$task->id])}}" method="POST">
<input type="hidden" name="_method" value="PUT">
{{ csrf_field() }}
<div class="row">
<div class="col-md-3" >
<label for="title" >title</label>
<input id="title" type="text" class="form-control" name="title" value="{{$task->title}}" required autofocus>
</div>
<div class="col-md-3">
<label for="description" >description</label>
<input id="description" type="text" class="form-control" name="description" value="{{$task->description}}" required>
</div>
</div>
<br>
<div class="row">
<div class="col-md-12">
<button type="submit" class="btn btn-primary" >
Edit
</button>
</div>
</div>
</form>
Error :
Symfony \ Component \ HttpKernel \ Exception \ MethodNotAllowedHttpException
No message
In your routes file the you have declared the wrong method . it should be like this.
Route::put('/{task}', 'HomeController#update')->name('update');
You are using wrong method type in routes as compare to method type in form,
Route::post('/{task}/', 'HomeController#update')->name('update');
Should work.
I'm creating a web site. And I have created a registration page. I want to update my details.
But, It gives me this error and I have also uploaded a picture of errors below. -
Error Picture
ErrorException (E_ERROR) Trying to get property of non-object (View:
D:\wamp64\www\FinalProject\resources\views\AdminUpdate.blade.php)
I used dd($edd); and it gave me correct details. But, when I try with below codes it gives me that above error.
How can I Fix this ??
Here is my AdminPanel.blade.php
<table class="table table-bordered">
<tr>
<td> Name </td>
</tr>
#foreach($data as $value )
<tr>
<td> {{ $value->username }} </td>
<td> <input type="submit" name="update" value="Update" class="btn-primary"> </td>
</tr>
#endforeach
</table>
Here is my AdminPanelController.php
public function edit($id)
{
$edd = User::find($id);
//dd($edd);
return view('AdminUpdate', ['edd' => $edd]);
}
public function adminedit($id, Request $request, User $user)
{
// Add Validation
$users = $user->find($id);
$users->username = $request->get('username');
$users->email = $request->get('email');
$users->save();
return redirect()->back();
}
Here is my AdminUpdate.blade.php
<form action="edit/{{ $edd[0]->id }}" method="post" enctype="multipart/form-data">
{{ method_field('PUT') }}
{{ csrf_field() }}
<div class="form-group">
<label>Username : *</label>
<input type="text" class="form-control" name="username" value="{{$edd[0]->username}}" placeholder="Enter Your Username" required>
</div>
<div class="form-group">
<label>Email : *</label>
<input type="email" class="form-control" name="email" value="{{$edd[0]->email}}" placeholder="Enter Your Username" required>
</div>
<div class="form-group">
<label>Password : *</label>
<input type="password" class="form-control" name="password" value="{{$edd[0]->password}}" placeholder="Enter Your Password" required>
</div>
<div class="form-group">
<label>Upload Profile Picture :</label>
<input type="file" class="form-control-file" name="file_img" aria-describedby="fileHelp">
<small id="fileHelp" class="form-text text-muted">If U Want , U Can Skip Upload A Profile Picture</small>
</div>
<input type="submit" class="btn btn-primary" value="Update User">
</form>
Here are my Routes.
Route::get('/edit/{id}', 'AdminPanelController#edit');
Route::post('/edit/{id}', 'AdminPanelController#adminedit');
User::find() gives you a single object, not array.
So, in blade just use
$edd->id
Same applies to other instances of $edd: username, email, password.
As a sidenote: you can add a check if user is really found by id and if not - show 404 page, for example.
You shouldn't write {{ $edd[0]->id }} instead of this use:
{{ $edd->id }}
Change { $edd[0]->id }} line to
{{ $edd->id }}
I have the following problem when trying to edit an "album", hopefully they can help me, I'm a little frustrated haha.
The Form
<form name="editalbum" action="{{ action('AlbumController#postEdit', $album->id) }}" method="POST" enctype="multipart/form-data">
{{ csrf_field() }}
<fieldset>
<h2>Editar <strong>{{$album->name}}</strong></h2>
<br></br>
<div class="form-group">
<label for="name">Nombre del proyecto</label>
<input name="name" type="text" class="form-control" value="{{ $album->name }}" required>
</div>
<div class="form-group">
<label for="description">Descripción del proyecto</label>
<textarea name="description" rows="10" cols="50" type="text" class="form-control" value="{{ $album->description }}" required></textarea>
</div>
<div class="form-group">
<label for="location">Locación:</label>
<input name="location" type="text" class="form-control" value="{{ $album->location }}" required>
</div>
<div class="form-group">
<label for="year">Año:</label>
<input name="year" type="text" class="form-control" value="{{ $album->year }}" required>
</div>
<button type="submit" class="btn btn-primary">Editar</button>
</fieldset>
</form>
So far I think everything is going well because I try to post in the ID of the model.
The function:
public function postEdit(Request $request, $id)
{
$album = Album::find($id);
$album = Album::all();
if(count($album) > 0){
$album->name = Input::get('name');
$album->description = Input::get('description');
$album->year = Input::get('year');
$album->location = Input::get('location');
$album->save();
Alert::success('Successfully Updated', 'Congratulations');
return view('admin.dashboard');
} else {
Alert::error('Facilities not found', 'Error');
return view('galeries');
}
I think you made error in routes.php
It should look like this:
Route::post('albums/update/{id}', ['uses' => 'AlbumController#postEdit']);
One solution will be to remove the DI Request object
public function postEdit($id)
{
//rest of code
}
note: the param has to be passed as a array
action="{{ action('AlbumController#postEdit', ['id' => $album->id]) }}"
This is how the registration form of FOSUserBundle looks like:
<form action="/Symfony/web/signup/" method="POST" class="fos_user_registration_register">
<div id="fos_user_registration_form">
<input type="hidden" id="fos_user_registration_form__token" name="fos_user_registration_form[_token]" value="c248f3ef17b082803ae9948c03d137c380f0dc24"/>
<div>
<label for="fos_user_registration_form_username">Username:</label><input type="text" id="fos_user_registration_form_username" name="fos_user_registration_form[username]" required="required" maxlength="255" pattern=".{2,255}"/>
</div>
<div>
<label for="fos_user_registration_form_email">Email:</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">Password:</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">Verification:</label><input type="password" id="fos_user_registration_form_plainPassword_second" name="fos_user_registration_form[plainPassword][second]" required="required"/>
</div>
</div>
<div>
<input type="submit" value="Register"/>
</div>
So, as you can see,
<input type="email" id="fos_user_registration_form_email" name="fos_user_registration_form[email]"
MAIN QUESTION: How can I change the id to something like id="email" and also the name to something like name="email"? And it has to work, obviously.
Here you can see: https://github.com/FriendsOfSymfony/FOSUserBundle/blob/master/Resources/views/Registration/register_content.html.twig {{ form_widget(form) }}, but I can't trace this to where it goes. I also presume the RegistrationFormHandler would have to be edited to support these parameters.
Alter buildForm function in RegistrationFormType class:
# FOSUserBundle/Form/Type/RegistrationFormType.php
class RegistrationController extends ContainerAware
{
// ...
public function getName()
{
return 'fos_user_registration';
}
}
Change fos_user_registration to whatever you want.