Nasik Ahd
Nasik Ahd

Reputation: 798

Laravel Form Validator required_if

I am facing a problem with required_if that, when i need to validate if type_id = 3 external_id should be required.

$validator = Validator::make($request->all(), [
        'memberID'=> 'required', 
        'external_id'=>'required_if:type_id,3'
]);

error message will be

The External id field is required when type id is 3.

validation working fine. But instead of type id is 3 i need to display a description. like below shown.

The External id field is required when type is category.

how to do that?

Upvotes: 0

Views: 150

Answers (1)

Md.Sukel Ali
Md.Sukel Ali

Reputation: 3065

You can do that by create a custom validation message,

$customMessages = [
        'required_if' => 'The :attribute field is required when type is category'
    ];


$validator = Validator::make($request->all(), [
        'memberID'=> 'required', 
        'external_id'=>'required_if:type_id,3'
], $customMessages);

Upvotes: 2

Related Questions