Skip to content

Commit

Permalink
Merge pull request #5199 from overtrue/4.2
Browse files Browse the repository at this point in the history
[4.2] add setter and getter for Validatior
  • Loading branch information
taylorotwell committed Sep 10, 2014
2 parents bc0a1c6 + a2296cb commit 23461a6
Show file tree
Hide file tree
Showing 2 changed files with 98 additions and 1 deletion.
55 changes: 55 additions & 0 deletions src/Illuminate/Validation/Validator.php
Original file line number Diff line number Diff line change
Expand Up @@ -2156,6 +2156,19 @@ public function setAttributeNames(array $attributes)

return $this;
}

/**
* Set the custom values on the validator.
*
* @param array $values
* @return \Illuminate\Validation\Validator
*/
public function setValueNames(array $values)
{
$this->customValues = $values;

return $this;
}

/**
* Get the files under validation.
Expand Down Expand Up @@ -2249,6 +2262,48 @@ public function setCustomMessages(array $messages)
{
$this->customMessages = array_merge($this->customMessages, $messages);
}

/**
* Get the custom attributes for the validator
*
* @return array
*/
public function getCustomAttributes()
{
return $this->customAttributes;
}

/**
* Add the custom attributes for the validator
*
* @param array $customAttributes
* @return void
*/
public function addCustomAttributes(array $customAttributes)
{
$this->customAttributes = array_merge($this->customAttributes, $customAttributes);
}

/**
* Get the custom values for the validator
*
* @return array
*/
public function getCustomValues()
{
return $this->customValues;
}

/**
* Add the custom values for the validator
*
* @param array $customValues
* @return void
*/
public function addCustomValues(array $customValues)
{
$this->customValues = array_merge($this->customValues, $customValues);
}

/**
* Get the fallback messages for the validator.
Expand Down
44 changes: 43 additions & 1 deletion tests/Validation/ValidationValidatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,17 @@ public function testAttributeNamesAreReplaced()
$this->assertFalse($v->passes());
$v->messages()->setFormat(':message');
$this->assertEquals('Name is required!', $v->messages()->first('name'));

//set customAttributes by setter
$trans = $this->getRealTranslator();
$trans->addResource('array', array('validation.required' => ':attribute is required!'), 'en', 'messages');
$customAttributes = array('name' => 'Name');
$v = new Validator($trans, array('name' => ''), array('name' => 'Required'));
$v->addCustomAttributes($customAttributes);
$this->assertFalse($v->passes());
$v->messages()->setFormat(':message');
$this->assertEquals('Name is required!', $v->messages()->first('name'));


$trans = $this->getRealTranslator();
$trans->addResource('array', array('validation.required' => ':attribute is required!'), 'en', 'messages');
Expand Down Expand Up @@ -147,7 +158,38 @@ public function testDisplayableValuesAreReplaced()
$this->assertFalse($v->passes());
$v->messages()->setFormat(':message');
$this->assertEquals('type must be included in Short, Long.', $v->messages()->first('type'));


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

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


Expand Down

0 comments on commit 23461a6

Please sign in to comment.