I have a form where a user can update several profile fields such as username, email, location, etc... I am having trouble implementing the password section... it seems overkill to have a separate form for 2 password fields so i'd like to check if the user enters their old password then it will collect the new password and update it, unfortunately it's not working..
/**
* Update the users profile
*/
public function update($username)
{
$user = User::whereUsername($username)->firstOrFail();
if(!Hash::check(Input::get('old_password'), $user->password)) // checking for old password
{
$input = Input::only('username', 'email', 'location', 'twitter_username', 'soundcloud_username'); // if not preset don't validate the password field
}
else
{
$input = Input::only('username', 'email', 'location', 'twitter_username', 'soundcloud_username', 'password', 'password_confirmation'); // else do validate the password field
}
$rules = [
'username' => 'required|unique:users,username,'.$user->id,
'email' => 'required|email|unique:users,email,'.$user->id,
'password' => 'sometimes|required|confirmed' // only validate password if it has been entered
];
$validator = Validator::make($input, $rules);
if ($validator->fails())
{
echo $validator->messages();
}
else
{
if(Hash::check(Input::get('old_password'), $user->password)) // check again and add the new hash to the array
{
$user->password = Hash::make(Input::get('password'));
}
$user->update($input); // update?
$user->save(); // finally save?
Flash::success('Your profile has been updated.');
return Redirect::route('profile', $user->username);
}
}
I definitely think i've gone the long way about it.. any ideas?
I made a similar thing just a few days ago - users can enter a new password and a password confirmation to change password, but they can also leave that field empty (and not change anything). I did this:
$rules["passwd_confirm"] = "required_with:passwd|same:passwd";
And if is passes validation, this snippet changes the password if needed
if(Input::get('passwd') != "") {
$user->password = Hash::make(Input::get('passwd'));
}
Hope this helps
I didn't need to use the validation rule as added confirm takes care of that but thanks i managed to get it working using your method.
Sign in to participate in this thread!
The Laravel portal for problem solving, knowledge sharing and community building.
The community