Skip to content

Fix attribute name used on Validator instance within certain rule classes #54845

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Feb 28, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 28 additions & 13 deletions src/Illuminate/Validation/Validator.php
Original file line number Diff line number Diff line change
Expand Up @@ -307,11 +307,11 @@ class Validator implements ValidatorContract
protected $defaultNumericRules = ['Numeric', 'Integer', 'Decimal'];

/**
* The current placeholder for dots in rule keys.
* The current random hash for the validator.
*
* @var string
*/
protected $dotPlaceholder;
protected static $placeholderHash;

/**
* The exception to throw upon failure.
Expand Down Expand Up @@ -344,7 +344,9 @@ public function __construct(
array $messages = [],
array $attributes = [],
) {
$this->dotPlaceholder = Str::random();
if (! isset(static::$placeholderHash)) {
static::$placeholderHash = Str::random();
}

$this->initialRules = $rules;
$this->translator = $translator;
Expand Down Expand Up @@ -372,7 +374,7 @@ public function parseData(array $data)

$key = str_replace(
['.', '*'],
[$this->dotPlaceholder, '__asterisk__'],
['__dot__'.static::$placeholderHash, '__asterisk__'.static::$placeholderHash],
$key
);

Expand Down Expand Up @@ -410,7 +412,7 @@ protected function replacePlaceholders($data)
protected function replacePlaceholderInString(string $value)
{
return str_replace(
[$this->dotPlaceholder, '__asterisk__'],
['__dot__'.static::$placeholderHash, '__asterisk__'.static::$placeholderHash],
['.', '*'],
$value
);
Expand All @@ -425,7 +427,7 @@ protected function replacePlaceholderInString(string $value)
protected function replaceDotPlaceholderInParameters(array $parameters)
{
return array_map(function ($field) {
return str_replace($this->dotPlaceholder, '.', $field);
return str_replace('__dot__'.static::$placeholderHash, '.', $field);
}, $parameters);
}

Expand Down Expand Up @@ -746,7 +748,7 @@ protected function getPrimaryAttribute($attribute)
protected function replaceDotInParameters(array $parameters)
{
return array_map(function ($field) {
return str_replace('\.', $this->dotPlaceholder, $field);
return str_replace('\.', '__dot__'.static::$placeholderHash, $field);
}, $parameters);
}

Expand Down Expand Up @@ -872,11 +874,24 @@ protected function hasNotFailedPreviousRuleIfPresenceRule($rule, $attribute)
*/
protected function validateUsingCustomRule($attribute, $value, $rule)
{
$attribute = $this->replacePlaceholderInString($attribute);
$originalAttribute = $this->replacePlaceholderInString($attribute);

$attribute = match (true) {
$rule instanceof Rules\Email => $attribute,
$rule instanceof Rules\File => $attribute,
$rule instanceof Rules\Password => $attribute,
default => $originalAttribute,
};

$value = is_array($value) ? $this->replacePlaceholders($value) : $value;

if ($rule instanceof ValidatorAwareRule) {
if ($attribute !== $originalAttribute) {
$this->addCustomAttributes([
$attribute => $this->customAttributes[$originalAttribute] ?? $originalAttribute,
]);
}

$rule->setValidator($this);
}

Expand All @@ -889,14 +904,14 @@ protected function validateUsingCustomRule($attribute, $value, $rule)
get_class($rule->invokable()) :
get_class($rule);

$this->failedRules[$attribute][$ruleClass] = [];
$this->failedRules[$originalAttribute][$ruleClass] = [];

$messages = $this->getFromLocalArray($attribute, $ruleClass) ?? $rule->message();
$messages = $this->getFromLocalArray($originalAttribute, $ruleClass) ?? $rule->message();

$messages = $messages ? (array) $messages : [$ruleClass];

foreach ($messages as $key => $message) {
$key = is_string($key) ? $key : $attribute;
$key = is_string($key) ? $key : $originalAttribute;

$this->messages->add($key, $this->makeReplacements(
$message, $key, $ruleClass, []
Expand Down Expand Up @@ -1189,7 +1204,7 @@ public function getRulesWithoutPlaceholders()
{
return (new Collection($this->rules))
->mapWithKeys(fn ($value, $key) => [
str_replace($this->dotPlaceholder, '\\.', $key) => $value,
str_replace('__dot__'.static::$placeholderHash, '\\.', $key) => $value,
])
->all();
}
Expand All @@ -1203,7 +1218,7 @@ public function getRulesWithoutPlaceholders()
public function setRules(array $rules)
{
$rules = (new Collection($rules))->mapWithKeys(function ($value, $key) {
return [str_replace('\.', $this->dotPlaceholder, $key) => $value];
return [str_replace('\.', '__dot__'.static::$placeholderHash, $key) => $value];
})->toArray();

$this->initialRules = $rules;
Expand Down
49 changes: 49 additions & 0 deletions tests/Integration/Validation/Rules/EmailValidationTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php

namespace Illuminate\Tests\Integration\Validation\Rules;

use Illuminate\Support\Facades\Validator;
use Illuminate\Validation\Rules\Email;
use Orchestra\Testbench\TestCase;
use PHPUnit\Framework\Attributes\TestWith;

class EmailValidationTest extends TestCase
{
#[TestWith(['0'])]
#[TestWith(['.'])]
#[TestWith(['*'])]
#[TestWith(['__asterisk__'])]
public function test_it_can_validate_attribute_as_array(string $attribute)
{
$validator = Validator::make([
'emails' => [
$attribute => '[email protected]',
],
], [
'emails.*' => ['required', Email::default()->rfcCompliant()],
]);

$this->assertTrue($validator->passes());
}

#[TestWith(['0'])]
#[TestWith(['.'])]
#[TestWith(['*'])]
#[TestWith(['__asterisk__'])]
public function test_it_can_validate_attribute_as_array_when_validation_should_fails(string $attribute)
{
$validator = Validator::make([
'emails' => [
$attribute => 'taylor[at]laravel.com',
],
], [
'emails.*' => ['required', Email::default()->rfcCompliant()],
]);

$this->assertFalse($validator->passes());

$this->assertSame([
0 => __('validation.email', ['attribute' => sprintf('emails.%s', str_replace('_', ' ', $attribute))]),
], $validator->messages()->all());
}
}
54 changes: 54 additions & 0 deletions tests/Integration/Validation/Rules/FileValidationTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?php

namespace Illuminate\Tests\Integration\Validation\Rules;

use Illuminate\Http\UploadedFile;
use Illuminate\Support\Facades\Validator;
use Illuminate\Validation\Rules\File;
use Orchestra\Testbench\TestCase;
use PHPUnit\Framework\Attributes\TestWith;

class FileValidationTest extends TestCase
{
#[TestWith(['0'])]
#[TestWith(['.'])]
#[TestWith(['*'])]
#[TestWith(['__asterisk__'])]
public function test_it_can_validate_attribute_as_array(string $attribute)
{
$file = UploadedFile::fake()->create('laravel.png', 1, 'image/png');

$validator = Validator::make([
'files' => [
$attribute => $file,
],
], [
'files.*' => ['required', File::types(['image/png', 'image/jpeg'])],
]);

$this->assertTrue($validator->passes());
}

#[TestWith(['0'])]
#[TestWith(['.'])]
#[TestWith(['*'])]
#[TestWith(['__asterisk__'])]
public function test_it_can_validate_attribute_as_array_when_validation_should_fails(string $attribute)
{
$file = UploadedFile::fake()->create('laravel.php', 1, 'image/php');

$validator = Validator::make([
'files' => [
$attribute => $file,
],
], [
'files.*' => ['required', File::types($mimes = ['image/png', 'image/jpeg'])],
]);

$this->assertFalse($validator->passes());

$this->assertSame([
0 => __('validation.mimetypes', ['attribute' => sprintf('files.%s', str_replace('_', ' ', $attribute)), 'values' => implode(', ', $mimes)]),
], $validator->messages()->all());
}
}
49 changes: 49 additions & 0 deletions tests/Integration/Validation/Rules/PasswordValidationTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php

namespace Illuminate\Tests\Integration\Validation\Rules;

use Illuminate\Support\Facades\Validator;
use Illuminate\Validation\Rules\Password;
use Orchestra\Testbench\TestCase;
use PHPUnit\Framework\Attributes\TestWith;

class PasswordValidationTest extends TestCase
{
#[TestWith(['0'])]
#[TestWith(['.'])]
#[TestWith(['*'])]
#[TestWith(['__asterisk__'])]
public function test_it_can_validate_attribute_as_array(string $attribute)
{
$validator = Validator::make([
'passwords' => [
$attribute => 'secret',
],
], [
'passwords.*' => ['required', Password::default()->min(6)],
]);

$this->assertTrue($validator->passes());
}

#[TestWith(['0'])]
#[TestWith(['.'])]
#[TestWith(['*'])]
#[TestWith(['__asterisk__'])]
public function test_it_can_validate_attribute_as_array_when_validation_should_fails(string $attribute)
{
$validator = Validator::make([
'passwords' => [
$attribute => 'secret',
],
], [
'passwords.*' => ['required', Password::default()->min(8)],
]);

$this->assertFalse($validator->passes());

$this->assertSame([
0 => sprintf('The passwords.%s field must be at least 8 characters.', str_replace('_', ' ', $attribute)),
], $validator->messages()->all());
}
}
Loading