I am binding directly to the model property. I am unable to submit the form as there are two forms in a single livewire component. 1st form is to edit the user, which consist of name, email, role, and the 2nd form is to invite the user, which consist of email.
I am unable to submit the form as I have combinedly stated the rules for both the forms under the rules protected property. So when ever I try to edit a user, using the first form, It checks for email field as well, which is present in the invitation form. How can I make it work in this structure.
- App\Http\Livewire\UserController.php
public $showUserManagementModal = false;
public $showUserInvitationModal = false;
public User $user;
public Invitation $invitation;
protected function rules() {
if ($showUserManagementModal = true)
return [
'user.name' => 'required | string | max:255',
'user.email' => 'required | string | email | max:255',
'role' => 'required',
];
if ($showUserInvitationModal = true)
return [
'invitation.email' => 'required | string | email | max:255 | unique:invitations,email',
];
}
public function createInvitation() {
$this -> useCachedRows();
$this -> resetValidation();
$this -> invitation = new Invitation();
$this -> showUserInvitationModal = true;
}
public function saveInvitation() {
$this -> validate();
$this -> invitation -> generateInvitationToken();
$this -> invitation -> save();
$this -> showUserInvitationModal = false;
}
public function manageUser(User $user) {
$this -> useCachedRows();
$this -> resetValidation();
$this -> user = $user;
$this -> role = $user -> roles -> pluck('id');
$this -> showUserManagementModal = true;
}
public function saveUser() {
$this -> validate();
$this -> validate([
'user.email' => 'unique:users,email,'.$this -> user -> id,
]);
$this -> user -> roles() -> sync($this -> role);
$this -> user -> save();
$this -> showUserManagementModal = false;
$this -> dispatchBrowserEvent('notify', $this -> user -> name.' Updated Successfully');
}
rules()method, you are assigning a value oftrueto the variable$showUserManagementModal. If you wanted to do a comparison, that's a different operator and it would fail because you haven't defined the variable anywhere. You also aren't using that method anywhere.$showUserManagementModalfrom false to true, when ever an admin tries to manage a user.