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

Synchro's avatar

required_without, sometimes, nullable, custom rules

I'm trying to validate a request that must provide at least one of otp and qrid fields. I'm using required_without to enforce that, but I'm not clear on how it interacts with sometimes and nullable and my custom validators:

        'otp'          => ['required_without:qrid', 'sometimes', 'nullable', new OTP(), 'exists:otps,otp'],
        'qrid'         => ['required_without:otp', 'sometimes', 'nullable', new QrId(), 'exists:otps,qrid'],

if either of these are missing or null, I get an error from my custom validator, which does not allow null values – but as far as I can I can see, the validation should have been allowed to pass before it gets to that point. Do I need these rules in a different order or something? Or is there a "stop validating if null" rule?

I don't really want to allow null at all, I've just seen it recommended for "at least one of" situations like this, and this.

0 likes
3 replies
SilenceBringer's avatar

@synchro it should be

        'otp'          => ['nullable', 'required_without:qrid', new OTP(), 'exists:otps,otp'],
        'qrid'         => ['nullable', 'required_without:otp', new QrId(), 'exists:otps,qrid'],
1 like
Synchro's avatar

Unfortunately that's still throwing the same error. My Rule instances don't (and shouldn't) allow null, but I'm looking for a way to make it stop evaluating according to the require_without rule. I don't think it's special to my Rules; I suspect that a string rule at the same point would fail in the same way.

Synchro's avatar

i tracked it down: my rule validation method accepted only a string, and it's bailing due to type enforcement. It could still return false for a null value and remain correct. I was not expecting it to be called at all.

So I made that change and now my tests pass. Thanks for the help.

Please or to participate in this conversation.