[12.x] Fix conditional validation error messages to show expected values #57679
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Fix conditional validation error messages to show expected values
Description
This PR fixes a bug where conditional validation rules (
required_if,prohibited_if,accepted_if,declined_if,missing_if,present_if) display the actual submitted value in error messages instead of the expected value specified in the validation rule.The Problem [#57678]
When using conditional validation rules, error messages were showing what the user submitted rather than what the rule expects, creating confusing messages:
Additional Issues
Case sensitivity:
Boolean conversion:
The Solution
Changed line 21 in
src/Illuminate/Validation/Concerns/ReplacesAttributes.php:protected function replaceAcceptedIf($message, $attribute, $rule, $parameters) { - $parameters[1] = $this->getDisplayableValue($parameters[0], Arr::get($this->data, $parameters[0])); + $parameters[1] = $this->getDisplayableValue($parameters[0], $parameters[1]); $parameters[0] = $this->getDisplayableAttribute($parameters[0]); return $this->replaceWhileKeepingCase($message, ['other' => $parameters[0], 'value' => $parameters[1]]); }Instead of fetching the actual value from submitted data (
$this->data), we now use the expected value from the rule definition ($parameters[1]).Affected Rules
This fix improves error messages for all rules that use
replaceAcceptedIf():required_ifprohibited_ifaccepted_ifdeclined_ifmissing_ifpresent_ifTest Coverage
New Tests
Added comprehensive test suite in
tests/Validation/ValidationConditionalRuleErrorMessagesTest.php:Total: 14 new test cases
Updated Tests
Updated 4 existing tests in
ValidationValidatorTest.phpthat were asserting the old (buggy) behavior:testRequiredIf(line 1818)testProhibitedIf(line 2025)testValidateAcceptedIf(line 2759)testValidateDeclinedIf(line 3112)These tests now correctly expect the rule's expected value rather than the submitted value.
Examples
Example 1: Required If
Example 2: Boolean Fields
Example 3: Case Sensitivity
Breaking Changes
✅ NO BREAKING CHANGES - This is a safe, non-breaking change that only affects error message content, not validation behavior or API.
Why This Is Safe
What Changed:
Impact Analysis
For 99.99% of Applications:
Extremely Rare Edge Cases (< 0.1%):
This change qualifies as a bug fix that improves accuracy, not a breaking change.
Related Issues
Fixes: [Issue #57678]