简体   繁体   English

在laravel5.6中将图像上传字段设为可选

[英]Make image upload field as optional in laravel5.6

Need to make image upload field as optional but I don't know how to make this and I googled I got some info but I don't understand how to make that. 需要将图像上传字段设置为可选字段,但我不知道如何进行此操作,我用谷歌搜索了一些信息,但我不知道如何进行此操作。 I used 我用了

$rules = ['image'=>'sometimes|mimes:jpeg,jpg|max:191|image',
'Image'=>'image'];

 Validator::make($request[image],$rules);

If you want to leave field validation as optional, just leave: 如果要将字段验证保留为可选,只需离开:

$rules = [
     'image' => 'mimes:jpeg,jpg|max:191|image'
];

Validator::make($request->all(), $rules);

Now, this field is only validated if it is present in the input array. 现在,仅当输入数组中存在该字段时,该字段才被验证。

When you add "sometimes", it only performs validation when this field is the only one present in the input array. 当您添加“有时”时,仅当此字段是输入数组中唯一存在的字段时,它才执行验证。 If you have for example "image" and "imagetype" on input arrray, the "image" will not be validated. 例如,如果在输入错误处有“ image”和“ imagetype”,则“ image”将不被验证。

See the docs about "sometimes": https://laravel.com/docs/5.6/validation#conditionally-adding-rules 请参阅有关“有时”的文档: https : //laravel.com/docs/5.6/validation#conditionally-adding-rules

` `

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM