【问题标题】:Laravel validation rule to force user to fill at least one input out of threeLaravel 验证规则强制用户填写三个输入中的至少一个
【发布时间】:2021-11-09 17:31:12
【问题描述】:

我的刀片中有一个带有 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>

【问题讨论】:

    标签: laravel validation laravel-blade


    【解决方案1】:

    在您的控制器中,您可以验证您的请求并使用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',
    ]);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-08-24
      • 1970-01-01
      • 2018-03-20
      • 2014-06-08
      • 2014-07-01
      相关资源
      最近更新 更多