0

I have validation in Laravel 10 that contains validation for company_description. I want it to be required when two other fields have specified values. I got this code:

'company_description' => [
                'string',
                'nullable',
                function ($attribute, $value, $fail) {
                    if (request()->input('offer_type_id') == 1 && request()->input('use_default_company_description') == false) {
                        if (empty($value) || $value == '') {
                            $fail('Company description is required.');
                        }
                    }
                },
            ],

but its not working. If nullable is removed its working, but I need this field to be nullable. What am I doing wrong?

3
  • You're checking for empty($value) || $value == '', which is what nullable handles, so your Rules overlap. Commented Sep 23, 2024 at 13:32
  • @TimLewis I dont get it. So how I can force company_description to have value if offer_type_id is 1 and use_default_company_description is false, and be nullable otherwise? Commented Sep 23, 2024 at 13:47
  • Your $fail() code can never be triggered if you keep nullable, since you're checking for empty($value) || $value == '', which nullable already handles... You need to remove nullable and handle it manually in your rule, probably as an else { ... } statement. Commented Sep 23, 2024 at 13:50

2 Answers 2

1

Use the withValidator and a sometimes to conditionally add the required rule.

company_description' => ['string']

public function withValidator($validator) {
        $validator->sometimes('company_description', 'required', function ($input) {
            return $input->offer_type_id == 1 && $input->use_default_company_description == false;
        });
    }
Sign up to request clarification or add additional context in comments.

Comments

0

nullable allows the field to be null, which oppose the custom validation rule you are trying to apply when offer_type_id == 1 and use_default_company_description == false. When nullable is present, it takes precedence

'company_description' => [
        'string',
        'nullable',
        function ($attribute, $value, $fail) {
            $offerType = request()->input('offer_type_id');
            $useDefault = request()->input('use_default_company_description');
    
            // If offer_type_id is 1 and use_default_company_description is false
            if ($offerType == 1 && $useDefault == false) {
                // Check if the company_description is null or empty
                if (!request()->filled('company_description')) {
                    $fail('Company description is required.');
                }
            }
        },
    ],

Kindly note the following

  1. request()->filled(): This helper checks if the input is present and not empty or null
  2. The fail() method will only be triggered if company_description is empty or not provided when offer_type_id == 1 and use_default_company_description == false

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.