Reputation: 267
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.
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');
}
Upvotes: 0
Views: 1372
Reputation: 26490
There are two issues with your rules()
method - one being that you are checking for a local variable (its not checking a property of the class) and secondly you're assigning values to the variable instead of comparing (single =
instead of double ==
).
protected function rules() {
if ($this->showUserManagementModal === true)
return [
'user.name' => 'required|string|max:255',
'user.email' => 'required|string|email|max:255',
'role' => 'required',
];
if ($this->showUserInvitationModal === true)
return [
'invitation.email' => 'required|string|email|max:255|unique:invitations,email',
];
}
You should also move the unique-check for the email to the rules()
method instead of checking that explicitly in your saveUser()
method.
protected function rules() {
if ($this->showUserManagementModal === true)
return [
'user.name' => 'required|string|max:255',
'user.email' => 'required|string|email|max:255|unique:users,email'.($this->user->id ? ','.$this->user->id : ''),
'role' => 'required',
];
if ($this->showUserInvitationModal === true)
return [
'invitation.email' => 'required|string|email|max:255|unique:invitations,email',
];
}
Upvotes: 2