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

6

5

I have a field "image", type is file and 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.

I tried still with parameters present and sometimes but the field is still required.

How can I validate it only if image is selected?

Placencia answered 11/1, 2017 at 10:43 Comment(0)
H
18
  • If the "avatar" field may not be present in the input array you want to use:

'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 sometimes rule.


  • If the "avatar" field will absolutely be in the input array and it's value will be then null you want to use:

'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 nullable rule.

Heyduck answered 11/1, 2017 at 10:50 Comment(0)
T
2

In your case, You have to check only if it is present or not null - Validating When there is value. So use 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.

$v = Validator::make($data, array(
    'avatar' => 'sometimes',
));
Trainbearer answered 11/1, 2017 at 10:58 Comment(0)
O
0

You could try 'nullable' in your rules.

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

Ostmark answered 11/1, 2017 at 10:46 Comment(0)
R
0

First check the $request object. Check avatar is available or not in your request. Then try this:

'avatar' => 'sometimes|image|mimes:jpeg,bmp,png,gif|max:2048'
Rotherham answered 11/1, 2017 at 10:59 Comment(0)
M
0

the solution i found that works best is using Rule::requiredIf

'cover' => [
    'image',
    Rule::requiredIf(function () {
        if (some_condition) {
            return false;
        }

        return true;
    })
]
Matteson answered 5/7, 2020 at 22:56 Comment(0)
T
0

In case the image can be null, or string(in case of editing only other data) or image

public function rules(): array
    {
        return [
            'name' => 'required|string|max:255',
            'image' => 'nullable|' . (is_file($this->image) ? 'image|mimes:jpeg,png,jpg,gif|max:2048' : ''),
        ];
    }
Their answered 27/7 at 11:24 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.