Change the user information without changing the password if the password field is empty and edit it if we fill it in with confirmation of compliance

I am using lareavel 5 and I want to create a to update user information

my form has 4 fields name, email, password and confirmation password

validation rule for the desired name, email address is valid and required, and password, min: 6 characters and match with confirmation.

now everything is all right.

what i want to do:

when the user fills in the password, then the confirmation field must be filled in and matched. but if the user does not fill in the password, it will go through and will not check the error and update the user information without changing the password.

confirmation 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',   
         $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']);



any other better solution i have used.


source to share

1 answer

You can use validation rules required_with

and same

. This makes both fields interdependent, and both or neither need to be filled.

'password' => 'required_with:password_confirmation|same:password_confirmation|min:6',   
'password_confirmation' => 'required_with:password|min:6',   




All Articles