Skip to content

Commit

Permalink
Add replaceDimensions for validator messages
Browse files Browse the repository at this point in the history
 * Added tests for that
  • Loading branch information
Hugome committed Feb 16, 2017
1 parent 0b1d587 commit a8e8b75
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 0 deletions.
21 changes: 21 additions & 0 deletions src/Illuminate/Validation/Concerns/ReplacesAttributes.php
Original file line number Diff line number Diff line change
Expand Up @@ -355,4 +355,25 @@ protected function replaceAfterOrEqual($message, $attribute, $rule, $parameters)
{
return $this->replaceBefore($message, $attribute, $rule, $parameters);
}

/**
* Replace all place-holders for the dimensions rule.
*
* @param string $message
* @param string $attribute
* @param string $rule
* @param array $parameters
* @return string
*/
protected function replaceDimensions($message, $attribute, $rule, $parameters)
{
$nesteds = $this->parseNamedParameters($parameters);
if (is_array($nesteds)) {
foreach ($nesteds as $key => $value) {
$message = str_replace(':'.$key, $value, $message);
}
}

return $message;
}
}
20 changes: 20 additions & 0 deletions tests/Validation/ValidationValidatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,26 @@ public function testClassBasedCustomReplacers()
$this->assertEquals('replaced!', $v->messages()->first('name'));
}

public function testNestedAttributesAreReplacedInDimensions()
{
// Knowing that demo image.gif has width = 3 and height = 2
$uploadedFile = new \Symfony\Component\HttpFoundation\File\UploadedFile(__DIR__.'/fixtures/image.gif', '', null, null, null, true);

$trans = $this->getIlluminateArrayTranslator();
$trans->addLines(['validation.dimensions' => ':min_width :max_height :ratio'], 'en');
$v = new Validator($trans, ['x' => $uploadedFile], ['x' => 'dimensions:min_width=10,max_height=20,ratio=1']);
$v->messages()->setFormat(':message');
$this->assertTrue($v->fails());
$this->assertEquals('10 20 1', $v->messages()->first('x'));

$trans = $this->getIlluminateArrayTranslator();
$trans->addLines(['validation.dimensions' => ':width :height :ratio'], 'en');
$v = new Validator($trans, ['x' => $uploadedFile], ['x' => 'dimensions:min_width=10,max_height=20,ratio=1']);
$v->messages()->setFormat(':message');
$this->assertTrue($v->fails());
$this->assertEquals(':width :height 1', $v->messages()->first('x'));
}

public function testAttributeNamesAreReplaced()
{
$trans = $this->getIlluminateArrayTranslator();
Expand Down

0 comments on commit a8e8b75

Please sign in to comment.