Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

olabd's avatar
Level 1

Problem with dependent validation

I have to make a validation of the field 'cover' that is dependent on two another fields:

1. If selected value of 'digital_forms' equals 1 then:

-if value of 'is_cover' equals 1 then 'cover' may pass empty

-if value of 'is_cover' equals 0 then 'cover' must not be empty

2. If selected value of 'digital_forms' equals 0 then 'cover' can be empty

I have to do this in AppServiceProvider class in boot() function using Validator::extend()

I was trying to use sometimes() function on validator object but it doesn't seem to work.

Validator::extend('test', function($attribute, $value, $parameters, $validator){
           
 $validator->sometimes('cover', 'required_if:digital_forms,1', function($input) use ($attribute, $value, $parameters, $validator){
                return $input->is_cover != 1;
            });
        });

I might get the whole concept wrong, but have no clue where.

I am sorry for the styling, I am trying to fix it.

0 likes
1 reply

Please or to participate in this conversation.