0

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');
}
5
  • You need to add some extra fields on your edit form. So, when you submit that form - check these fields are exists then don't check the validation or whatever you don't want to execute on your save function. Commented Dec 23, 2020 at 19:50
  • @Tuhin How should I proceed with it, can you please help me out with this? Commented Dec 23, 2020 at 19:52
  • Before trying to build an app with Laravel, you may want to take a step back and learn the basics of PHP first. Just looking at the first line of your rules() method, you are assigning a value of true to 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. Commented Dec 23, 2020 at 20:01
  • Thanks for throwing some light on it @miken32, I have already declared it as false by default. How can I further make the validation to exclude the field if I am editing a user and exclude the other three fields when I am creating an invite. Commented Dec 23, 2020 at 20:05
  • And I am changing the state of the $showUserManagementModal from false to true, when ever an admin tries to manage a user. Commented Dec 23, 2020 at 20:12

1 Answer 1

2

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',
        ];
}
Sign up to request clarification or add additional context in comments.

1 Comment

Its working perfectly, its just when I try to shift the unique-check for the email to the rules() I get the following error, Typed property App\Http\Livewire\Backend\UserManagement\UserController::$user must not be accessed before initialization

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.