Search code examples
laravelvalidationlaravel-formrequest

Including a custom form request in Laravel prevents the controller from working correct


Description

I have a little user/role management system and want the admin to be able to change passwords and names of the users. So far it works with this code in the UsersController:

     public function update(request $request, User $user)
  {

    // dd($request);
    //Save roles
    $user->roles()->sync($request->roles);

    //Save user

    $user->name = $request->input("name");
    if ($request->input('password')) {
      $user->password = Hash::make($request->input('password'));
    }
    $user->save();

    return redirect()->route("admin.users.index");
  }

I created a form request called UpdateUser with this command php artisan make:request UpdateUser and brought it into the UsersController with use App\Http\Requests\UpdateUser; and changed public function update(request $request, User $user)... to public function update(UpdateUser $request, User $user)...

(I got the tutorial for this from this site)

UpdateUser looks like this and the basic functionality should be to verfiy that the password input and password confirm input are matching.

<?php

namespace App\Http\Requests;

use Illuminate\Foundation\Http\FormRequest;

class UpdateUser extends FormRequest
{
  /**
   * Determine if the user is authorized to make this request.
   *
   * @return bool
   */
  public function authorize()
  {
    return true;
  }

  /**
   * Get the validation rules that apply to the request.
   *
   * @return array
   */
  public function rules()
  {
    return [
      'name' => ['string', 'max:255', "unique:users"],
      'password' => ["nullable", 'string', 'min:6', 'confirmed'],
      'roles' => ['exists:roles,id'],
    ];
  }
}

Problem

Without UpdateUser it works. I can change the name, the password and the role. But with UpdateUser i can only change the name. Can't change the role or the password. When i try to change role and/or password and submit it only reloads the edit page.

So what could be the reason for that?

You can find my whole repo here


Solution

  • As in most of the cases it turns out, that it was a user fault ;)

    So what happened?

    In the edit blade i load the name from the user, i want to edit.

    {{Form::text("name", $user->name,  ["class" => "edit-input", "placeholder" => "Neuer Name..."])}}
    

    And when i only changed the role or the password without changing name, the UpdateUser claims, that the user already exists. Here is the corresponding code line from UpdateUser again:

    public function rules()
      {
        return [
          'name' => ['string', 'max:255', "unique:users"],
          'password' => ["nullable", 'string', 'min:6', 'confirmed'],
          'roles' => ['exists:roles,id'],
        ];
      }
    }
    

    The unique:users requirement leads to a simple reload of the edit page because the name obviously already exists. I now got it because i implemented displaying the error/success notifications.

    In the edit blade i simply replaced $user->name with "".