I am using lareavel 5, and I want to create a from for updating user info
my form has 4 fields
name, email, password and confirm password
validation rule on name required, email valid and required, and password required, min:6 chars and match with confirm.
everything for now is ok.
what I want to do is:
when user fill password then confirm field should be filled and matched. but if user does not fill password the submit will pass and no validation error and update user info without change password.
validation code:
$user = Input::all();
if (trim($user['password']) != "") {
$rules = array(
'name' => 'required|max:255',
'email' => 'required|email|max:255|unique:users,email,' . $id,
'password' => 'required|confirmed|min:6',
);
}
else{
$rules = array(
'name' => 'required|max:255',
'email' => 'required|email|max:255|unique:users,email,' . $id,
);
}
update user info code:
$user = User::findOrFail($id);
$user->name = $data['name'];
$user->email = $data['email'];
if (trim($data['password']) != "") {
$user->password = bcrypt($data['password']);
}
$user->save();
any other best solution that I used.