如果选择了“图像/文件”,则 Laravel 验证字段?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/41588700/
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-09-14 15:06:25  来源:igfitidea点击:

Laravel validation for field if "image/file" is selected?

phplaravellaravel-5laravel-5.3

提问by Michelle

I have a field "image", type is fileand need only to validate if image is selected, means it can be also empty.

我有一个字段“图像”,类型是文件,只需要验证是否选择了图像,这意味着它也可以为空。

I tried it so: 'avatar' => 'mimes:jpeg,jpg,png,gif|max:100000',but so it is also required.

我是这样试过的:'avatar' => 'mimes:jpeg,jpg,png,gif|max:100000',但它也是必需的。

I tried still with parameters presentand sometimesbut the field is still required.

我仍然尝试使用参数presentsometimes但该字段仍然是必需的。

How can I validate it only if image is selected?

如何仅在选择图像时验证它?

回答by Mihailo

  • If the "avatar"field may not be presentin the inputarray you want to use:
  • 如果您要使用的输入数组中可能不存在“头像”字段:

'avatar' => 'sometimes|mimes:jpeg,jpg,png,gif|max:100000'

'avatar' => 'sometimes|mimes:jpeg,jpg,png,gif|max:100000'

In some situations, you may wish to run validation checks against a field only if that field is present in the input array. To quickly accomplish this, add the sometimesrule.

在某些情况下,您可能希望仅当输入数组中存在该字段时才对该字段运行验证检查。要快速完成此操作,请添加有时规则。



  • If the "avatar"field will absolutelybe in the inputarray and it's value will be then nullyou want to use:
  • 如果“头像”字段绝对输入数组中并且它的值将是那么null您要使用:

'avatar' => 'nullable|mimes:jpeg,jpg,png,gif|max:100000'

'avatar' => 'nullable|mimes:jpeg,jpg,png,gif|max:100000'

The field under validation may be null. This is particularly useful when validating primitive such as strings and integers that can contain null values. To quickly accomplish this, add the nullablerule.

验证中的字段可能为null。这在验证可包含空值的字符串和整数等原语时特别有用。要快速完成此操作,请添加规则。

回答by Renjith V R

In your case, You have to check only if it is present or not null - Validating When there is value. So use sometimes

在您的情况下,您必须仅检查它是否存在或不为 null - Validating When there is value。所以用sometimes

"In some situations, you may wish to run validation checks against a field only if that field is present in the input array. To quickly accomplish this, add the sometimes rule to your rule list"

“在某些情况下,您可能希望仅当输入数组中存在该字段时才对该字段运行验证检查。要快速完成此操作,请将有时规则添加到您的规则列表中”

 $v = Validator::make($data, array(
        'email' => 'sometimes|required|email',
    ));

In your case,

在你的情况下,

$v = Validator::make($data, array(
        'avatar' => 'sometimes|mimes:jpeg,jpg,png,gif|max:100000',
    ));

Note:

笔记:

The following code will do nothing because there are no rules to validate against, done because even if it's present in the POST array, you should specify rules. and the doesn't make any difference.

下面的代码什么都不做,因为没有要验证的规则,这样做是因为即使它存在于 POST 数组中,您也应该指定规则。并且没有任何区别。

$v = Validator::make($data, array(
    'avatar' => 'sometimes',
));

回答by Abdullah Al Shakib

First check the $requestobject. Check avataris available or not in your request. Then try this:

首先检查$request对象。avatar在您的请求中检查是否可用。然后试试这个:

'avatar' => 'sometimes|image|mimes:jpeg,bmp,png,gif|max:2048'

回答by Loek

You could try 'nullable'in your rules.

你可以试试'nullable'你的规则。

https://laravel.com/docs/5.3/validation#rule-nullable

https://laravel.com/docs/5.3/validation#rule-nullable