php 如果字段为空,则 Laravel 验证规则需要另一个字段

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/41805597/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 02:28:21  来源:igfitidea点击:

Laravel validation rules if field empty another field required

phplaravelvalidation

提问by Marco

I have a Laravel 5.3 app, and an update form where I send user profile values. One of them is user image as well, I am creating a hidden input field for it, so if the user is not uploading any new image, I can still see that he has the hidden field with the old image value.

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

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

If he removes the image, the hidden input value becomes empty.

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

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

That is how I thought of updating a user image. But I don't know how to set up validation rules for that form. Something that would be similar to this:

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

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

I have tried with 'image' => 'required_without:old_image',and also with 'image' => 'required_if:old_image, null',but none of them showed any error message for missing image. I am showing error messages like this:

我已经尝试过'image' => 'required_without:old_image',,也尝试过,'image' => 'required_if:old_image, null',但没有一个显示任何丢失图像的错误消息。我正在显示这样的错误消息:

回答by Richelly Italo

You can work with two rules

您可以使用两个规则

'image' => 'required_if:old_image'

Or:

或者:

'image' => 'required_without:old_image'

回答by Mohannad Najjar

this is can be done by required_withoutrule, if it's not working please show an actual sample request data ( dd(request()->all() )) and the validation rules you are using. the scenario you want to catch is simple: if both fields is empty.

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

in your example, applying this rule should work:

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

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

required_without:foo,bar,...

The field under validation must be present and not empty only when any of the other specified fields are not present. Validation - Laravel 5.3

required_without:foo,bar,...

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

Example:

例子:

$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."]]

回答by Ramzan Mahmood

Try this

尝试这个

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