【问题标题】:Laravel validation rules if field empty another field required如果字段为空需要另一个字段,Laravel 验证规则
【发布时间】:2017-06-07 22:02:33
【问题描述】:

我有一个 Laravel 5.3 应用程序和一个更新表单,用于发送用户个人资料值。其中之一也是用户图像,我正在为它创建一个隐藏的输入字段,所以如果用户没有上传任何新图像,我仍然可以看到他有带有旧图像值的隐藏字段。

<input type="hidden" class="form-control" name="old_image" value="{{ $player->image_filename }}" id="oldImage">

如果他移除图像,隐藏的输入值变为空。

document.getElementById('oldImage').value = '';

这就是我想到更新用户图像的方式。但我不知道如何为该表单设置验证规则。与此类似的东西:

    public function rules()
    {
        return [
          'first_name' => 'required|max:50',
          'last_name' => 'required|max:50',
          'birthday' => 'required',
          'image' => required if old_image empty
        ];
    }

我尝试过使用'image' =&gt; 'required_without:old_image','image' =&gt; 'required_if:old_image, null',,但它们都没有显示任何丢失图像的错误消息。我正在显示这样的错误消息:

【问题讨论】:

标签: php laravel validation


【解决方案1】:

您可以使用两条规则

'image' => 'required_if:old_image'

或者:

'image' => 'required_without:old_image'

【讨论】:

  • 请删除这个多余的逗号。
  • 完成!谢谢@MouhammedElshaaer
  • 不再起作用:验证规则 required_if 至少需要 2 个参数
  • required_if:old_image,null
  • Atm (Laravel 8) required_if:x,nullrequired_without:x 不同
【解决方案2】:

这可以通过required_without 规则来完成,如果它不起作用,请显示实际的样本请求数据(dd(request()-&gt;all() ))和您正在使用的验证规则。你想捕捉的场景很简单:如果两个字段都是空的。

在您的示例中,应用此规则应该有效:

return [
          // ...
          'image' => 'required_without:old_image'
        ];

required_without:foo,bar,...

验证中的字段必须存在且不为空,仅当有 其他指定的字段不存在。 Validation - Laravel 5.3

例子:

$rules = [
        'new_value' => 'required_without:old_value'
        ];

validator(
        ['new_value' =>'',
        'old_value' => 'old value is preserved here']
    , $rules)->errors()->toArray();

// output: [] <-- no errors!

validator(
        ['new_value' =>'New value!',
        'old_value' => '']
    , $rules)->errors()->toArray();

// output: [] <-- no errors!

validator(
        ['new_value' =>'',
        'old_value' => '']
    , $rules)->errors()->toArray();

// output: ["new_value" => ["The new value field is required when old value is not present."]]

【讨论】:

  • 这应该是公认的答案。一些与 Laravel 规则的集成需要数组配置(例如 Folklore GraphQL),任何非纯验证都是不够的。
【解决方案3】:

对于新的laravel版本,如果需要使用required_if,它至少需要两个参数,所以你应该使用第二个和第三个参数作为if条件。

  'old_image'=>'required_if:image,=,null',
  'image'=>'required_if:old_image,=,null'

【讨论】:

    【解决方案4】:

    使用这个

    'image'=>'required_if:old_image,=,null'
    

    【讨论】:

      【解决方案5】:

      试试这个

      if (empty($request->input('old_image'))){
           $this->validate($request, [
             'image' => 'required',
           ]);
      }
      else {
           // Validation in case of else goes here
           $this->validate($request, [
            // rule
           ]);
      }
      

      【讨论】:

      【解决方案6】:

      您缺少可空验证

      'old_image' => 'required'
      'image' => 'required_without:old_image|nullable'
      

      【讨论】:

        【解决方案7】:

        我知道这是一个老问题,但我在搜索过程中遇到了这个问题。所以我也会在这里发布我的解决方案,以供未来的访问者使用。

        我认为这个解决方案是 OP 想要的。此解决方案不使用其他包或自定义验证方法,因此 imo 也是一个优点。

        我做的是这样的:

        public function rules()
        {
            return [
              'first_name' => ['required', 'max:50'],
              'last_name' => ['required', 'max:50'],
              'birthday' => ['required'],
              'old_image' => ['nullable'],
              'image' => [
                  Rule::requiredIf(function() {
                      return empty($this->request->get('old_image'));
                  })
              ]
            ];
        }
        
        

        【讨论】:

          猜你喜欢
          • 2016-12-18
          • 2022-01-17
          • 2021-12-10
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2022-01-14
          • 2019-05-18
          • 1970-01-01
          相关资源
          最近更新 更多