0
votes

I have one form where I have 5 text fields and 2 input type file fields. for all 7 fields, I have written custom validation rules in laravel and I want all field required if a type is a business so I used required_if in all fields, for text field it's working but for an image (input type file) it's not working it always consider as file present in request and not give any error if file not uploaded when I changed it to required than it only gives me error for input type file.

public function rules()
{
    return [
        'country'       => 'required_if:type,business',
        'country2'      => 'required_if:type,business',
        'company'       => 'required_if:type,business',
        'number' => 'required_if:type,business',
        'expiry' => 'required_if:type,business',
        'profile_pic'       => 'required_if:type,business  |  mimes:jpeg,jpg,png,pdf',
        'document_pic'      => 'required_if:type,business  |  mimes:jpeg,jpg,png,pdf',
    ];
}
2
for country, country2, company, number,expiry its working but only for image its not give me error.joy
Read this Validating Arrays . You can use 'required_if:type,business | image'Exterminator

2 Answers

0
votes

The issue is coming because of extra space invalidation rules. Removed space between required if and mime type and its working properly.

public function rules()
{
    return [
        'country'       => 'required_if:type,business',
        'country2'      => 'required_if:type,business',
        'company'       => 'required_if:type,business',
        'number' => 'required_if:type,business',
        'expiry' => 'required_if:type,business',
        'profile_pic'       => 'required_if:type,business|mimes:jpeg,jpg,png,pdf',
        'document_pic'      => 'required_if:type,business|mimes:jpeg,jpg,png,pdf',
    ];
}
0
votes

You can try this:

public function rules()
{
    return [
        'country'       => 'required_if:type,business',
        'country2'      => 'required_if:type,business',
        'company'       => 'required_if:type,business',
        'number'        => 'required_if:type,business',
        'expiry'        => 'required_if:type,business',
        'profile_pic'   => 'required_if:type,business|image',
        'document_pic'  => 'required_if:type,business|image',
    ];
}

Also can edit mine :https://laravel.com/docs/5.3/validation#rule-mimes