[12.x] Fix validation wildcard array message type error#59339
Merged
taylorotwell merged 2 commits intolaravel:12.xfrom Mar 24, 2026
Merged
Conversation
…tching rule When a custom validation message is defined for a wildcard attribute key (e.g. students.*.grade) using a nested array of rule-specific messages, and the failing rule is not in that array, getFromLocalArray returned the entire array instead of null. This caused a TypeError in replaceInputPlaceholder (introduced in v12.55.1) which calls str_contains() on the message, expecting a string. The fix makes the wildcard branch consistent with the exact-match branch which already returns $message[$lowerRule] ?? null. Fixes laravel#59316
SanderMuller
pushed a commit
to SanderMuller/framework
that referenced
this pull request
Mar 26, 2026
* [12.x] Fix TypeError when wildcard custom message is array without matching rule When a custom validation message is defined for a wildcard attribute key (e.g. students.*.grade) using a nested array of rule-specific messages, and the failing rule is not in that array, getFromLocalArray returned the entire array instead of null. This caused a TypeError in replaceInputPlaceholder (introduced in v12.55.1) which calls str_contains() on the message, expecting a string. The fix makes the wildcard branch consistent with the exact-match branch which already returns $message[$lowerRule] ?? null. Fixes laravel#59316 * Add test for wildcard custom message on unmatched array rules
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Description
This PR resolves a fatal
TypeErrorcrash in the validator logic when developers define custom property-based array messages using wildcards, and a separate unregistered rule fails on an index of that array (Issue #59316).🚨 The Bug
When defining custom messages for nested properties, developers often use the array wildcard approach:
If an input like
fields.0fails validation for a completely different rule missing in that custom array (for example, in), the app crashes with a fatalTypeError:This happens because the system correctly extracts the array containing the custom message, attempts to match an absent array-key, silently returns the array structure instead of a translation string, and funnels the entire un-stringified array directly into
replacePlaceholders(...).💡 The Solution
During getFromLocalArray() resolution in FormatsMessages.php, we added a safe verification boundary:
if (is_array($source))check handles the edge case of array-format validation configurations securely. If a given wildcard local custom array match holds an entire array mapping and not specifically a string value, we elegantlyreturn null.This gracefully delegates the missing validation failure to standard laravel language lines instead of attempting to parse an array object through string replace methods. It inherently mirrors the exact behavior standard non-wildcard error paths use to fallback to default language files.
🤔 Why is this safe? Does it break anything?
This is completely safe and backwards compatible.
This condition strictly guards an explicit
is_arrayvariable type wherestringwas absolutely mandated to be passed into a method argument. Returningnullgracefully defers the unmatchedfields.*validation line up the delegation chain to the standard validation lines precisely how standard Eloquent mappings fallback!👩💻 How does it make building web applications easier?
When building heavy, dynamic form builders (especially SPAs and heavily-iterated Livewire components), developers lean extensively into array wildcards to define custom nested messages. A single validation misconfiguration resulting in a crash instead of a 422 JSON validation error is massively disruptive in production environments. Providing graceful language delegation keeps API consumption safe and predictable.
✅ Tests Added
I've implemented a concrete, specific test within ValidationValidatorTest called testWildcardArrayCustomMessagesHandleMissingRulesGracefullyWhenAnotherRuleFails.
It builds a nested Validator with array-based message configuration and validates an unmatched rule constraint (
in:X) which definitively proves the regression is resolved and translates the result successfully rather than dumping an application-crashing TypeError.