Skip to content

Conversation

@florianraith
Copy link
Contributor

Extend placeholder capitalization beyond :attribute to other validator placeholders (e.g., :other in the same rule). This makes behavior consistent across placeholders and improves localization for languages such as German, where field names are typically capitalized.

// Example
'same' => 'The :Attribute field must match :Other.',

// Before: "The Password field must match :Other."
// After:  "The Password field must match Password confirmation."

This PR addresses the inconsistency where :Attribute was supported, but other placeholders (like :Other) were not.

The implementation is inspired by FormatMessages::replaceAttributePlaceholder() which implements the feature for :attribute

@florianraith florianraith changed the title Allow validator message placeholers to be capitalized [12.x] Allow validator message placeholers to be capitalized Oct 27, 2025
Comment on lines +25 to +29
return str_replace(
[':other', ':OTHER', ':Other', ':value', ':VALUE', ':Value'],
[$parameters[0], Str::upper($parameters[0]), Str::ucfirst($parameters[0]), $parameters[1], Str::upper($parameters[1]), Str::ucfirst($parameters[1])],
$message
);
Copy link
Contributor

@shaedrich shaedrich Oct 27, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would it make sense to move that logic to its own method and then reuse it?

private function replaceKeepCase($message, $placeholder, $parameter)
{
	$fn = [ Str::lower(...), Str::upper(...), Str::ucfirst(...) ];
	$cases = array_map(fn (callable $fn) => ':' . $fn($placeholder), $fn);
	$replacements = array_map(fn (callable $fn) => $fn($parameter), $fn);
	return str_replace($cases, $replacements, $message);
}

protected function replaceAcceptedIf($message, $attribute, $rule, $parameters)
{
    $parameters[1] = $this->getDisplayableValue($parameters[0], Arr::get($this->data, $parameters[0]));

    $parameters[0] = $this->getDisplayableAttribute($parameters[0]);

    return $this->replaceKeepCase(
		$this->replaceKeepCase($message, 'other', $parameters[0]),
		'value',
		$parameters[1],
	);
}

@taylorotwell taylorotwell merged commit fb9dae5 into laravel:12.x Oct 28, 2025
68 checks passed
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.

3 participants