I am currently working on the profile page wherein the user can change its password. I created a new function changePassword()
in the UserController
public function changePassword(Request $request, User $employee) {
$validator = Validator::make($data, [
'password' => 'required|string|min:6|confirmed',
'password_confirmation' => 'required|string|min:6|same:password'
]);
if ($validator->fails()) {
Session::flash('error', "Fill-out the form correctly. Try again!");
return redirect()->back()->withErrors($validator);
}
$employee->password = bcrypt($request->password);
$employee->save();
return view('users.show', ['employee'=>$employee]);
}
I created this new route in web.php
above the resource route of the User model
Route::put('users/{user}', 'UserController@changePassword')->name('users.changePassword');
I am getting a MethodNotAllowedHttpException
every time I click the submit button. I think the problem is the route but I am not sure. Is there a Laravel functionality aside the one that sends email, because I want the users to change its password without using email. Thanks!
Here is my form
{!! Form::model($employee, ['method'=>'PUT', 'route'=>['users.changePassword', $employee]]) !!}
Instead of put, use post in your route as below.
Route::post('users/{user}', 'UserController@changePassword')->name('users.changePassword');
And in your form have you passed the "_method" & CSRF token (CSRF is not required, if you have disabled it, but I recommend to use it always)?
If you are using Form and HTML class (ServiceProviders) for creating HTML & Forms in blades, use as below.
{!! Form::model($employee, ['method' => 'POST','route' => ['users.changePassword']]) !!}
--- Your Fields Code will go here ----
{!! Form::close() !!}
Hope it helps!!
You need to add PUT
http verb in your form field. If you are using laravelcollective:
{!! Form::model($employee, ['method' => 'put','route' =>
['users.changePassword']]) !!}
--- Your Fields Code will go here ----
{!! Form::close() !!}
Or
<form action="your-url" method="POST">
{{ method_field('PUT') }}
</form>
Do you set parameter for route?
{!! Form::Model($user, ['action' => ['UserController@changePassword', $user->id],'method' => 'PUT']) !!}
{!! Form::close() !!}
try this....
this code work in my project using laravel 5.3
ubahprofilecontroller.php
public function updatePwd($id, Request $request)
{
//cek password lama
$messages = array(
'password_lama.required'=>'Harap masukkan password',
'password_baru.required' => 'Password baru tidak boleh kosong',
'ulangi_password.required' => 'Harap ketikkan ulang password baru',
'ulangi_password.same' => 'Password baru dan konfirmasi password tidak cocok'
);
$rules = array (
'password_lama'=> 'required',
'password_baru'=> 'required',
'ulangi_password'=> 'required|same:password_baru'
);
$validator = Validator::make ( Input::all (), $rules, $messages );
if($validator->fails())
{
/*return Redirect('edit_password')
->withErrors($validator);*/
$password ='password';
return redirect('ubahPwd')->withErrors($validator)->withInput();
}
else
{
$check = User::where('id',$id)->first();
if (Input::get('password_lama') == $check->value)
{
if(Input::get('password_baru') == Input::get('ulangi_password'))
{
$check -> password = bcrypt(Input::get('password_baru'));
$check -> salt_password = Input::get('password_baru');
// save our duck
$check->save();
/*$msg = array('msg' => 'Password changed Successfully');*/
return redirect('ubahPwd')->with('success','Password berhasil diubah');
}
else
{
/*$msg = array('msg' => 'New password and Confirm password did not match');*/
return redirect('ubahPwd')->with('salah','Password baru dan konfirmasi password tidak sama');
}
}
else
{
/*$msg = array('msg' => 'Current password is incorrect');*/
/*return Redirect('edit_password')
->with('status-failed', 'Current password is incorrect');*/
return redirect('ubahPwd')->with('salah','Password lama salah');
}
}
}
profile.blade.php
<form class="form-horizontal" action="{{ url('/ubahPassword/update',Auth::user()->id )}}" method="POST">
<<div class="content" style="padding-left:50px;padding-right:50px">
<div class="form-group">
{{ csrf_field() }}
<label for="id" class="col-sm-3 control-label">ID</label>
<div class="col-sm-2">
<input type="text" class="form-control" id="id" name="id" value="{{ Auth::user()->id }}" readonly>
</div>
</div>
<div class="form-group">
<label for="username" class="col-sm-3 control-label">Password Lama</label>
<div class="col-sm-6">
<input type="password" class="form-control" id="username" name="password_lama">
@if ($errors->has('password_lama')) <p class="help-block" style="color:red;">{{ $errors->first('password_lama') }}</p> @endif
</div>
</div>
<div class="form-group">
<label for="inputEmail3" class="col-sm-3 control-label">Password baru</label>
<div class="col-sm-5">
<input type="password" class="form-control" id="email" name="password_baru">
@if ($errors->has('password_baru')) <p class="help-block" style="color:red;">{{ $errors->first('password_baru') }}</p> @endif
</div>
</div>
<div class="form-group">
<label for="inputEmail3" class="col-sm-3 control-label">Ulangi Password</label>
<div class="col-sm-5">
<input type="password" class="form-control" id="email" name="ulangi_password">
@if ($errors->has('ulangi_password')) <p class="help-block" style="color:red;">{{ $errors->first('ulangi_password') }}</p> @endif
</div>
</div>
<div class="form-group">
<button type="submit" class="btn btn-info pull-right col-sm-3">Simpan</button>
</div>
</div>
</form>
i hope my answer helping you....
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.