Skip to content

[12.x] Fix validation wildcard array message type error#59339

Merged
taylorotwell merged 2 commits intolaravel:12.xfrom
sadique-cws:fix/validation-wildcard-array-message-type-error
Mar 24, 2026
Merged

[12.x] Fix validation wildcard array message type error#59339
taylorotwell merged 2 commits intolaravel:12.xfrom
sadique-cws:fix/validation-wildcard-array-message-type-error

Conversation

@sadique-cws
Copy link
Contributor

Description

This PR resolves a fatal TypeError crash 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:

$messages = [
    'fields.*' => [
        'required' => 'This specific field is required'
    ]
];

If an input like fields.0 fails validation for a completely different rule missing in that custom array (for example, in), the app crashes with a fatal TypeError:

TypeError: Illuminate\Validation\Concerns\ReplacesAttributes::replacePlaceholders(): Argument #1 ($message) must be of type string, array given

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 elegantly return 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_array variable type where string was absolutely mandated to be passed into a method argument. Returning null gracefully defers the unmatched fields.* 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.

…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
@taylorotwell taylorotwell merged commit 67b64d9 into laravel:12.x Mar 24, 2026
72 checks passed
@GrahamCampbell GrahamCampbell changed the title Fix/validation wildcard array message type error [12.x] Fix validation wildcard array message type error Mar 24, 2026
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
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