我正在try 使用livewire 3对表单进行实时验证.根据livewire文档,当try 使用运行时语法(如Rule::password())时,您应该定义一个规则方法,而不是使用#Rule.当你想实时验证时,你应该使用wire:model.live或wire:model.blur.但是,我无法让验证实时运行.验证仅在我单击注册按钮时运行.

在我的Register livewire组件中,我定义了如下规则方法

public function rules() {
    return [
        'name' => ['required', 'string', 'min:2'],
        'email' => ['required', 'string', 'lowercase', 'email', 'max:255', 'unique:'.User::class],
        'password' => ['required', 'string', 'confirmed', Rules\Password::defaults()],
    ];
}

然后,在我的注册方法中,我确保调用了$This->Valify(),如下所示.

public function register(): void
{
    $validated = $this->validate();

    $validated['password'] = Hash::make($validated['password']);

    event(new Registered($user = User::create($validated)));

    auth()->login($user);

    $this->redirect(RouteServiceProvider::HOME, navigate: true);
}

同样在我看来,我确保以如下方式添加wire:mod.blur

<x-text-input wire:model.blur="name" id="name" class="block mt-1 w-full" type="text" name="name" required autofocus autocomplete="name" />
<x-input-error :messages="$errors->get('name')" class="mt-2" />

我做错了什么?

推荐答案

With Livewire 3 the rules provided in function rule() are not automatically applied in real time, they are only applied when $this->validate() is called
To get around this you can use the #[Rule] attribute for validation, on each property.
Otherwise you can use the updated() method to force validation using the validations defined in rules():

public function updated($prop) {
   $this->validateOnly($prop);
}

Php相关问答推荐

我如何知道BaseController中当前运行的是哪个控制器?

WooCommerce:有条件地更改特定区域的发货类别

Msgraph-sdk-php v2如何从返回对象中获取数据?

有没有办法为整个PHP应用程序启用严格类型

将部分产品排除在WooCommerce的计算附加费之外

从目录文件夹中获取文件并追加到 Select 下拉列表(选项)-wordpress/wc

在PHP中使用curl命令执行SCRAPYD操作.json不返回任何内容

在Laravel中创建辅助函数时的约定

根据WooCommerce中的自定义用户元数据值更改用户角色

用ajax获取文件 - CORS问题&;设置缓存?

execute_query 和prepare+execute 有什么区别?

Docker-用于部署的 PHP 应用程序映像无法在本地运行

当所有对象都属于同一类型时,我可以省略 PHP in_array() 中的 strict 参数吗?

如何从 PHP 中的 .txt 文件中列出今天生日的人的姓名?

判断类别是否存在 Shopware 6

PHP / DOM : 解析 HTML 以提取基于类的数​​据

为什么 8.0 之前的 PHP 版本拒绝命名空间Random\Namespace作为无效语法?

PHP Remedy API 调用以创建附件无效的条目(使用 Postman Works!)

在 EasyAdmin 中添加全局操作,将我的按钮放在表格下方而不是上方

如何在 php/laravel 中获取元素在数组中的位置