diff --git a/src/Illuminate/Validation/Validator.php b/src/Illuminate/Validation/Validator.php index bacaeb71178d..a13fd0f8bca7 100755 --- a/src/Illuminate/Validation/Validator.php +++ b/src/Illuminate/Validation/Validator.php @@ -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. @@ -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. diff --git a/tests/Validation/ValidationValidatorTest.php b/tests/Validation/ValidationValidatorTest.php index dd05869899a4..78f42dba6a21 100755 --- a/tests/Validation/ValidationValidatorTest.php +++ b/tests/Validation/ValidationValidatorTest.php @@ -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'); @@ -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')); }