I think I have all of this working for the most part. But I'm still a bit confused about how the implicit validation rules work, where attributes that are not present or have an empty value (documentation mentions "string") normal and custom validation rules are not run. Additionally, for this to work, all fields need to be required. https://laravel.com/docs/8.x/validation#implicit-rules Am I missing something?
My rules are currently stated thusly:
return [
'name' => 'required|string|min:3',
'email' => $this->userId
? ['required', 'email', Rule::unique('users')->ignore($this->userId)]
: 'required|email|unique:users,email',
'phone' => 'required|digits:11',
'password' => ['required', 'confirmed', Password::defaults()],
'selectedRoles' => 'required|array|min:1',
'selectedRoles.*' => 'required|string|distinct|min:1',
];
In my Store function:
$rules = $this->rules();
$input = [
'password' => '',
'password_confirmation' => ''
];
Validator::make($input, $rules)->passes();
$validatedData = $this->validate($this->rules());
$user = User::updateOrcreate([
'id' => $this->userId
],[
'name' => $validatedData['name'],
'email' => $validatedData['email'],
'phone' => $validatedData['phone'],
'password' => Hash::make($validatedData['password']),
]);
$user->assignRole($validatedData['selectedRoles']);
$updateOrCreateVerb = ($this->userId) ? 'updated' : 'created';
$this->success = "User {$user->name} was {$updateOrCreateVerb} successfully!";