Skip to content

[12.x] Add missing conditional validation rule builders#59209

Merged
taylorotwell merged 1 commit into
laravel:12.xfrom
SanderMuller:feature/conditional-validation-rules
Mar 16, 2026
Merged

[12.x] Add missing conditional validation rule builders#59209
taylorotwell merged 1 commit into
laravel:12.xfrom
SanderMuller:feature/conditional-validation-rules

Conversation

@SanderMuller

Copy link
Copy Markdown
Contributor

This PR adds the missing unless counterparts to the existing conditional validation rule builders (Rule::requiredIf(), Rule::excludeIf(), Rule::prohibitedIf()).

Motivation

Laravel provides Rule::requiredIf(), Rule::excludeIf(), and Rule::prohibitedIf() for closure/boolean-based conditional validation. However, the unless variants are missing. While you can negate the condition (Rule::requiredIf(! $isFree)), the unless form reads more naturally in many contexts and matches the naming convention users expect when they see the If variants exist.

Example usage

// Before: negate the condition manually
'coupon' => [Rule::excludeIf(! $hasSubscription), 'string']
'terms' => [Rule::requiredIf(! $isAdmin), 'accepted']
'legacy_field' => [Rule::prohibitedIf(! $allowLegacy)]

// After: express intent directly
'coupon' => [Rule::excludeUnless($hasSubscription), 'string']
'terms' => [Rule::requiredUnless($isAdmin), 'accepted']
'legacy_field' => [Rule::prohibitedUnless($allowLegacy)]

Works with closures too:

Rule::requiredUnless(fn () => $this->user()->isAdmin())
Rule::excludeUnless(fn () => $this->hasActiveSubscription())

Added

  • Rule::excludeUnless($callback) - excludes the field when condition is false
  • Rule::requiredUnless($callback) - requires the field when condition is false
  • Rule::prohibitedUnless($callback) - prohibits the field when condition is false

@taylorotwell taylorotwell merged commit 03afbad into laravel:12.x Mar 16, 2026
22 of 72 checks passed
jonagoldman pushed a commit to deplox/laravel-framework that referenced this pull request Apr 30, 2026
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.

2 participants