Skip to content
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

[8.x] Add placeholders replace for accepted_if validation message #38240

Merged
merged 2 commits into from Aug 5, 2021
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
18 changes: 18 additions & 0 deletions src/Illuminate/Validation/Concerns/ReplacesAttributes.php
Expand Up @@ -414,6 +414,24 @@ protected function replaceProhibitedUnless($message, $attribute, $rule, $paramet
return str_replace([':other', ':values'], [$other, implode(', ', $values)], $message);
}

/**
* Replace all place-holders for the accepted_if rule.
*
* @param string $message
* @param string $attribute
* @param string $rule
* @param array $parameters
* @return string
*/
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 str_replace([':other', ':value'], $parameters, $message);
}

/**
* Replace all place-holders for the same rule.
*
Expand Down
31 changes: 31 additions & 0 deletions tests/Validation/ValidationValidatorTest.php
Expand Up @@ -1806,6 +1806,37 @@ public function testValidateAcceptedIf()

$v = new Validator($trans, ['foo' => 'true', 'bar' => 'aaa'], ['foo' => 'accepted_if:bar,aaa']);
$this->assertTrue($v->passes());

// accepted_if:bar,aaa
$trans = $this->getIlluminateArrayTranslator();
$trans->addLines(['validation.accepted_if' => 'The :attribute field must be accepted when :other is :value.'], 'en');
$v = new Validator($trans, ['foo' => 'no', 'bar' => 'aaa'], ['foo' => 'accepted_if:bar,aaa']);
$this->assertFalse($v->passes());
$v->messages()->setFormat(':message');
$this->assertSame('The foo field must be accepted when bar is aaa.', $v->messages()->first('foo'));

// accepted_if:bar,aaa,...
$trans = $this->getIlluminateArrayTranslator();
$trans->addLines(['validation.accepted_if' => 'The :attribute field must be accepted when :other is :value.'], 'en');
$v = new Validator($trans, ['foo' => 'no', 'bar' => 'abc'], ['foo' => 'accepted_if:bar,aaa,bbb,abc']);
$this->assertFalse($v->passes());
$v->messages()->setFormat(':message');
$this->assertSame('The foo field must be accepted when bar is abc.', $v->messages()->first('foo'));

// accepted_if:bar,boolean
$trans = $this->getIlluminateArrayTranslator();
$trans->addLines(['validation.accepted_if' => 'The :attribute field must be accepted when :other is :value.'], 'en');
$v = new Validator($trans, ['foo' => 'no', 'bar' => false], ['foo' => 'accepted_if:bar,false']);
$this->assertFalse($v->passes());
$v->messages()->setFormat(':message');
$this->assertSame('The foo field must be accepted when bar is false.', $v->messages()->first('foo'));

$trans = $this->getIlluminateArrayTranslator();
$trans->addLines(['validation.accepted_if' => 'The :attribute field must be accepted when :other is :value.'], 'en');
$v = new Validator($trans, ['foo' => 'no', 'bar' => true], ['foo' => 'accepted_if:bar,true']);
$this->assertFalse($v->passes());
$v->messages()->setFormat(':message');
$this->assertSame('The foo field must be accepted when bar is true.', $v->messages()->first('foo'));
}

public function testValidateEndsWith()
Expand Down