简体   繁体   中英

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.

See the docs about "sometimes": https://laravel.com/docs/5.6/validation#conditionally-adding-rules

`

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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