Laravel Validation: use single field to validate a set fields

80 Views Asked by At

I have two fields that FormRequest is expecting to receive as a set: image_id and image_path.

However I want to only return a single message using the field image_path when the FormRequest receives a wrong input.

For example, image_id is string, or image_id is null, both are null, image_path is plain string etc.

Which ever error Laravel decides is an error, I would like Laravel to only say that image_path is required or image_path is not a url or image_path is not set correctly. (Custom message not required)

current validation rule:

$rules = [
    'image_id' => ['required', 'integer'],
    'image_path' => ['required', 'url', 'min:2', 'max:500'],
];

If I set it up as is, the user would receive two error validation message.

Note as per comment, I am unable to use $stopOnFirstFailure since it will also stop other validation errors. Also if the validation is on image_id the return error message key will be image_id.

0

There are 0 best solutions below