Skip to content

[12.x] Refine type hinting for condition rules#56420

Merged
taylorotwell merged 3 commits into
laravel:12.xfrom
michaelnabil230:required-if
Jul 26, 2025
Merged

[12.x] Refine type hinting for condition rules#56420
taylorotwell merged 3 commits into
laravel:12.xfrom
michaelnabil230:required-if

Conversation

@michaelnabil230

@michaelnabil230 michaelnabil230 commented Jul 25, 2025

Copy link
Copy Markdown
Contributor

Refine type hinting for condition rules

Comment thread src/Illuminate/Validation/Rules/RequiredIf.php Outdated
@michaelnabil230 michaelnabil230 changed the title [12.x] Refine type hinting for condition in RequiredIf rule [12.x] Refine type hinting for condition rules Jul 25, 2025
@taylorotwell taylorotwell merged commit 7ce337e into laravel:12.x Jul 26, 2025
60 checks passed
@michaelnabil230 michaelnabil230 deleted the required-if branch July 26, 2025 02:57
@patrickomeara

Copy link
Copy Markdown
Contributor

This is a breaking change for me. Originally Rule::requiredIf() would work with truthy values, not strict boolean/callback.

In my case I was applying the rule if a route param was present. Sure, I can cast it to boolean pretty simply but a breaking change in a patch version is not ideal, in a PR focusing on type hints nonetheless.

Rule::requiredIf($this->route('label'))

I agree that the original is_string() check was confusing, I can't see why string values were not allowed when we could pass other truthy values: ints, objects etc.

A better approach might be to accept any value, string or not, this too would be a breaking change, however only for those expecting an InvalidArgumentException to be thrown when a string is passed in, a pretty unlikely scenario.

If the strictness is to stay, there is no reason why native types can't be used.

    public function __construct(Closure|bool|null $condition)
    {
        $this->condition = $condition;
    }

My vote is to drop the strictness and allow any truthy/falsy value. Happy to PR something to fix this breaking change pending your opinion @taylorotwell

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants