0

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. Commented Sep 14, 2018 at 5:49
  • Read this Validating Arrays . You can use 'required_if:type,business | image' Commented Sep 14, 2018 at 5:53

2 Answers 2

1

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

Sign up to request clarification or add additional context in comments.

Comments

0

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',
    ];
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.