【问题标题】:Mount Method changed property not maintaining state in other methodsMount 方法更改的属性未在其他方法中保持状态
【发布时间】:2022-01-02 16:00:48
【问题描述】:

我正在更改 mount 方法中的规则属性。但是当我再次在其他方法中使用规则时,它不会保持状态。 createAccount 方法是使用 wire:click 从按钮调用的。

class CreateUsers extends Component
{
    use PasswordValidationRules;

    public User $user;

    protected $rules = [
        'user.name' => ['required', 'string', 'max:255'],
        'user.username' => ['required', 'string', 'max:255'],
        'user.confirm_password' => 'same:password',
        'user.password' => [],

    ];

    public function mount()
    {

        $this->rules['user.password'] = $this->passwordRules();
        dd($this->rule);  // here it is showing updated value
        $this->user = new User();
    }

    public function createAccount()
    {
        dd($this->rule);  // here it is showing default value
        $this->validate();
        $this->user->save();

    }


    public function render()
    {
        return view('livewire.create-users');
    }
}

【问题讨论】:

    标签: php laravel laravel-livewire livewires


    【解决方案1】:

    不使用rules属性,而是使用函数将passwordRules()添加进去,无需在mount方法中调用

    public function rules()
    {
       return [
            'user.name' => ['required', 'string', 'max:255'],
            'user.username' => ['required', 'string', 'max:255'],
            'user.confirm_password' => 'same:password',
            'user.password' => $this->passwordRules(),
       ];
    }
    

    【讨论】:

    • 是的,我现在使用它。但为什么它不工作?为什么调用 mount 方法后它的值会被重置?
    猜你喜欢
    • 2021-07-26
    • 1970-01-01
    • 1970-01-01
    • 2023-04-04
    • 1970-01-01
    • 2012-06-09
    • 2021-04-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多