Skip to content
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
45 changes: 45 additions & 0 deletions src/Illuminate/Validation/Validator.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,13 @@ class Validator implements MessageProviderInterface {
*/
protected $customAttributes = array();

/**
* The array of custom displayabled values.
*
* @var array
*/
protected $customValues = array();

/**
* All of the custom validator extensions.
*
Expand Down Expand Up @@ -1490,6 +1497,35 @@ protected function getAttribute($attribute)
}
}

/**
* Get the displayable name of the value.
*
* @param string $attribute
* @param mixed $value
* @return string
*/
public function getDisplayableValue($attribute, $value)
{
if (isset($this->customValues[$attribute][$value]))

Choose a reason for hiding this comment

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

There's no method to set $customValues like there is to set $customAttributes (unless one extends Validator class):

Custom attributes may be set either in constructor or using setAttributeNames(array $attributes).
I think this cool functionality deserves similar API.

To explain: I'm not using the default key validation.values because my models attributes overlap.

{
return $this->customValues[$attribute][$value];
}

$key = "validation.values.{$attribute}.{$value}";

// We allow for the developer to specify language lines for each of the
// values allowing for more displayable counterparts of each of
// the attribute's value. This provides the ability for simple formats.
if (($line = $this->translator->trans($key)) !== $key)
{
return $line;
}
else
{
return $value;
}
}

/**
* Replace all place-holders for the between rule.
*
Expand Down Expand Up @@ -1585,6 +1621,10 @@ protected function replaceMax($message, $attribute, $rule, $parameters)
*/
protected function replaceIn($message, $attribute, $rule, $parameters)
{
foreach ($parameters as &$parameter) {
$parameter = $this->getDisplayableValue($attribute, $parameter);
}

return str_replace(':values', implode(', ', $parameters), $message);
}

Expand All @@ -1599,6 +1639,10 @@ protected function replaceIn($message, $attribute, $rule, $parameters)
*/
protected function replaceNotIn($message, $attribute, $rule, $parameters)
{
foreach ($parameters as &$parameter) {
$parameter = $this->getDisplayableValue($attribute, $parameter);
}

return str_replace(':values', implode(', ', $parameters), $message);
}

Expand Down Expand Up @@ -1675,6 +1719,7 @@ protected function replaceRequiredWithoutAll($message, $attribute, $rule, $param
*/
protected function replaceRequiredIf($message, $attribute, $rule, $parameters)
{
$parameters[1] = $this->getDisplayableValue($parameters[0], $parameters[1]);
$parameters[0] = $this->getAttribute($parameters[0]);

return str_replace(array(':other', ':value'), $parameters, $message);
Expand Down
22 changes: 22 additions & 0 deletions tests/Validation/ValidationValidatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,28 @@ public function testAttributeNamesAreReplaced()
$this->assertEquals('Name is required!', $v->messages()->first('name'));
}

public function testDisplayableValuesAreReplaced()
{
//required_if:foo,bar
$trans = $this->getRealTranslator();
$trans->addResource('array', array('validation.required_if' => 'The :attribute field is required when :other is :value.'), 'en', 'messages');
$trans->addResource('array', array('validation.values.color.1' => 'red'), 'en', 'messages');
$v = new Validator($trans, array('color' => '1', 'bar' => ''), array('bar' => 'RequiredIf:color,1'));
$this->assertFalse($v->passes());
$v->messages()->setFormat(':message');
$this->assertEquals('The bar field is required when color is red.', $v->messages()->first('bar'));

//in:foo,bar,...
$trans = $this->getRealTranslator();
$trans->addResource('array', array('validation.in' => ':attribute must be included in :values.'), 'en', 'messages');
$trans->addResource('array', array('validation.values.type.5' => 'Short'), 'en', 'messages');
$trans->addResource('array', array('validation.values.type.300' => 'Long'), 'en', 'messages');
$v = new Validator($trans, array('type' => '4'), array('type' => 'in:5,300'));
$this->assertFalse($v->passes());
$v->messages()->setFormat(':message');
$this->assertEquals('type must be included in Short, Long.', $v->messages()->first('type'));

}

public function testCustomValidationLinesAreRespected()
{
Expand Down