【问题标题】:Laravel require at least one fieldLaravel 至少需要一个字段
【发布时间】:2016-06-29 02:26:21
【问题描述】:

我正在构建一个表单。有 3 个特定文本文件,其中至少一个应由用户填写。我如何使用 Laravel 验证规则来实现它?

 //dd($request)

 array:6 [▼
 "_token" => "o5td5RMv2EQ5mA7LqXpyMXCKIu7L78BfrRSEU1se"
"skill_id" => "6"
 "plan_id" => "1"
 //at least one of below fields should be filled
 "context" => ""
 "link" => ""
 "desired_date" => ""
 ]

【问题讨论】:

    标签: validation laravel rules


    【解决方案1】:

    你可以使用:

    required_without_all:foo,bar,...

    只有在所有其他指定字段都不存在时,才必须存在正在验证的字段。

    $rules = array(
        'skill_id' => 'required_without_all:plan_id,context,link',
        'plan_id' => 'required_without_all:skill_id,context,link',
    );
    

    您可以使用required_unless 规则:https://laravel.com/docs/5.2/validation#rule-required-unless

    required_unless:anotherfield,value,...

    在这种情况下,必须存在正在验证的字段,除非 anotherfield 字段等于任何值。

    【讨论】:

      【解决方案2】:

      您可以使用required_without_all

      $rules = array(
          'context' => 'required_without_all:link,desired_date',
          'link' => 'required_without_all:context,desired_date',
          'desired_date' => 'required_without_all:context,link',
      );
      

      【讨论】:

        猜你喜欢
        • 2014-06-17
        • 2011-07-21
        • 2015-08-12
        • 2020-08-20
        • 1970-01-01
        • 2023-03-10
        • 2017-06-02
        • 2018-10-06
        • 1970-01-01
        相关资源
        最近更新 更多