Laravel 强制用户至少填写三项输入中的一项的验证规则

Laravel validation rule to force user to fill at least one input out of three

我的 blade 中有一个表单,其中包含 3 个输入。用户必须至少填写一个输入,我的意思是如果其他输入为空,则每个输入都是必需的。我不知道如何在 Laravel 控制器中编写我的验证规则。输入:

                       <div class="mt-3">
                            <x-label for="telegram" value="__('Telegram')"/>
                            <x-input
                                type="text" name="telegram"
                                class="mt-1 block w-full"
                                autofocus/>
                        </div>

                        <div class="mt-3">
                            <x-label for="whatsapp" value="__('Whatsapp')"/>
                            <x-input
                                type="text" name="whatsapp"
                                class="mt-1 block w-full"
                                autofocus/>
                        </div>

                        <div class="mt-3">
                            <x-label for="discord" value="__('Discord')"/>
                            <x-input 
                                  type="text" name="discord"
                                  class="mt-1 block w-full"
                                  autofocus/>
                        </div>

在您的控制器中,您可以验证您的请求并使用 required_without_all 规则。

required_without_all:foo,bar,...
The field under validation must be present and not empty only when all of the other specified fields are empty or not present.
$validated = $request->validate([
        'telegram' => 'required_without_all:whatsapp,discord',
        'whatsapp' => 'required_without_all:telegram,discord',
        'discord' => 'required_without_all:telegram,whatsapp',
]);