I've got some problems when I want to show validation errors when I got multiple forms on the same page.
As you can see below, I've got 2 forms (general and password) on the same page.
| | GET|HEAD | profile/edit/settings | | App\Http\Controllers\ProfileController@getEditSettings | web,auth |
| | POST | profile/edit/settings/general | | App\Http\Controllers\ProfileController@postEditSettings | web,auth |
| | POST | profile/edit/settings/password | | App\Http\Controllers\ProfileController@postEditPassword | web,auth |
Each form has a hidden input field like this: <input type="hidden" name="password2" value="1">
and at the bottom of the view, I've got this:
@if (!empty(old('password2')))
@include('errors.common')
@endif
This is so that I don't display the same errors on both forms. But no errors are showing whatsoever.
And if I dd(old('password2'))
I get null
.
In my ProfileController I have this method:
/**
* @return mixed
* Processing new password
*/
public function postEditPassword() {
// Grab signed user
$user = Auth::user();
// Change password
$user -> changePassword(Input::all());
}
As you can see above, I try to change the password with my custom method in the User model, which looks like this:
public function changePassword($data) {
// The signed user
$user = Auth::user();
// Try to match the old password
if (!Hash::check($data['old_password'], $user -> password)) {
return redirect() -> back() -> with('error', 'Det gamle passordet stemmer ikke.');
}
// Validate the new password
$validator = Validator::make([
'password' => $data['password'],
'password_confirm' => $data['password_confirm']
], [
'password' => 'required|min:6',
'password_confirm' => 'required|min:6|same:password'
]);
if ($validator -> fails()) {
return redirect() -> back() -> withErrors($validator);
}
// Change the password
$user -> password = bcrypt($data['password']);
$user -> save();
return redirect() -> back() -> with('success', 'Passordet ble endret!');
}
Also, besides the errors view problem, I was wondering if this is a good approach with processing different forms (take data in controller, send to model for processing and the model will then redirect).