Laravel Fortify::通过Livewire调用' updatePassword() '函数



我正在使用Livewire来构建我的Laravel项目,并且我也在使用Fortify实现安全/身份验证层。

由于各种原因,我没有使用Jetstream。话虽如此,Jetstream显然采用了一些非常有用的功能和技术来完成它的工作,因此本着真正的创造力精神,我"借用"了它。下面的"update-password"作为Jetstream如何使用Livewire与Fortify接口的示例。

这个HTML模板是实际HTML代码的简化版本,用于构建我的表单,但我已经去掉了所有的样式和标签复杂性等,以便我们可以只关注重要的内容:

<!-- update-password-form.blade.php (Livewire Template) -->
<form wire:submit.prevent="updatePassword">
<input id="current_password" type="password" name="current_password" required="true" />
<input id="password" type="password" name="password" required="true" />
<input id="password_confirmation" type="password" name="password_confirmation" required="true" />
<button type="submit">Change Password</button>
</form>

正如我们所看到的,Livewire阻止了表单的默认提交,并将其重定向到Livewire类中的updatePassword()函数。您在这里看到的函数是这样的:

/**
* Update the user's password.
*
* @param  LaravelFortifyContractsUpdatesUserPasswords  $updater
* @return void
*/
public function updatePassword(UpdatesUserPasswords $updater)
{
$this->resetErrorBag();
$updater->update(Auth::user(), $this->state);
$this->state = [
'current_password' => '',
'password' => '',
'password_confirmation' => '',
];
$this->emit('saved');
}

这一切似乎工作得很好。当我(用户)按[Change Password]Livewire设置表单不活动(以防止用户双重提交表单),当从Laravel/Livewire收到响应时,表单再次启用。和…好了,就这样了。

问题是我提交什么数据并不重要。如果我输入所有正确的值,密码不会更新!如果我输入不正确的current_password,它不会出错。如果我提交正确的current_password,passwordpassword_confirmation不匹配,我得到相同的"什么也没发生";经验(作为最终用户)。当我检查"网络"时;选项卡,我每次得到有效的200个响应,没有任何明显的错误报告的细节。我知道PHP函数被调用,因为如果我把一个dd($this)风格的调试在它,JSON响应抛出Livewire对象。

所以我的问题是…

如何强化框架管理错误,我应该如何抓住他们(在Livewire)给用户一些有用的反馈?

是否ErrorBag(在第一行代码中重置)以某种方式在$updater->update()函数中填充?

此外,我从Jetstream项目中复制(抱歉,借用)了这个解决方案。这是否也意味着Jetstream界面同样不直观(从最终用户的角度来看)?在Jetstream项目中,我是否遗漏了一些更高层次的概念?

我太蠢了。错误包返回给视图,只是模板中没有outlet来显示响应。我所需要的只是一个有条件的label(或spandiv),以便在错误存在时显示该字段。

<form wire:submit.prevent="updatePassword">
<input id="current-password" type="password" name="current_password" required="true" />
@error('current_password')
<label id="current-password-err" class="error" for="current-password">{{ $message }}</label>
@enderror
<input id="password" type="password" name="password" required="true" />
@error('password')
<label id="password-err" class="error" for="password">{{ $message }}</label>
@enderror
<input id="password-confirmation" type="password" name="password_confirmation" required="true" />
@error('password_confirmation')
<label id="password-confirmation-err" class="error" for="password-confirmation">{{ $message }}</label>
@enderror
<button type="submit">Change Password</button>
</form>

最新更新