From 5a6c10d5fafaf5a19e085b6b1b16d9aff9714d80 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=AE=89=E6=AD=A3=E8=B6=85?= Date: Wed, 23 Jul 2014 11:52:17 +0800 Subject: [PATCH 1/6] add getter and setter --- src/Illuminate/Validation/Validator.php | 42 +++++++++++++++++++++++++ 1 file changed, 42 insertions(+) diff --git a/src/Illuminate/Validation/Validator.php b/src/Illuminate/Validation/Validator.php index 944ba7cf75d1..a15df7e62da7 100755 --- a/src/Illuminate/Validation/Validator.php +++ b/src/Illuminate/Validation/Validator.php @@ -2212,6 +2212,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; + } + + /** + * Set the custom attributes for the validator + * + * @param array $customAttributes + * @return void + */ + public function setCustomAttributes(array $customAttributes) + { + $this->customAttributes = array_merge($this->customAttributes, $customAttributes); + } + + /** + * Get the custom values for the validator + * + * @return array + */ + public function getCustomValues() + { + return $this->customValues; + } + + /** + * Set the custom values for the validator + * + * @param array $customValues + * @return void + */ + public function setCustomValues(array $customValues) + { + $this->customValues = array_merge($this->customValues, $customValues); + } /** * Get the fallback messages for the validator. From 9b7eea0fa73c0bc359bec0132ccb8afec66084a0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=AE=89=E6=AD=A3=E8=B6=85?= Date: Wed, 23 Jul 2014 11:55:16 +0800 Subject: [PATCH 2/6] add test for #5a6c10d --- tests/Validation/ValidationValidatorTest.php | 28 +++++++++++++++++++- 1 file changed, 27 insertions(+), 1 deletion(-) diff --git a/tests/Validation/ValidationValidatorTest.php b/tests/Validation/ValidationValidatorTest.php index f665c3bb61fc..8801a387e8eb 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->setCustomAttributes($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,22 @@ public function testDisplayableValuesAreReplaced() $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->setCustomValues($customValues); + $this->assertFalse($v->passes()); + $v->messages()->setFormat(':message'); + $this->assertEquals('type must be included in Short, Long.', $v->messages()->first('type')); } From 25e01bd160101410d92fd45387328e0039d10eaf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=AE=89=E6=AD=A3=E8=B6=85?= Date: Wed, 23 Jul 2014 16:58:09 +0800 Subject: [PATCH 3/6] fix method name, add method 'setValueNames' 'set' -> 'add' --- src/Illuminate/Validation/Validator.php | 21 +++++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-) diff --git a/src/Illuminate/Validation/Validator.php b/src/Illuminate/Validation/Validator.php index a15df7e62da7..dfc628298fbe 100755 --- a/src/Illuminate/Validation/Validator.php +++ b/src/Illuminate/Validation/Validator.php @@ -2119,6 +2119,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->customAttributes = $values; + + return $this; + } /** * Get the files under validation. @@ -2224,12 +2237,12 @@ public function getCustomAttributes() } /** - * Set the custom attributes for the validator + * Add the custom attributes for the validator * * @param array $customAttributes * @return void */ - public function setCustomAttributes(array $customAttributes) + public function addCustomAttributes(array $customAttributes) { $this->customAttributes = array_merge($this->customAttributes, $customAttributes); } @@ -2245,12 +2258,12 @@ public function getCustomValues() } /** - * Set the custom values for the validator + * Add the custom values for the validator * * @param array $customValues * @return void */ - public function setCustomValues(array $customValues) + public function addCustomValues(array $customValues) { $this->customValues = array_merge($this->customValues, $customValues); } From 39f3378b64db1635a43c6278346bd9e4b56d4d42 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=AE=89=E6=AD=A3=E8=B6=85?= Date: Wed, 23 Jul 2014 16:59:18 +0800 Subject: [PATCH 4/6] update test --- tests/Validation/ValidationValidatorTest.php | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/tests/Validation/ValidationValidatorTest.php b/tests/Validation/ValidationValidatorTest.php index 8801a387e8eb..f0a1d417a605 100755 --- a/tests/Validation/ValidationValidatorTest.php +++ b/tests/Validation/ValidationValidatorTest.php @@ -122,7 +122,7 @@ public function testAttributeNamesAreReplaced() $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->setCustomAttributes($customAttributes); + $v->addCustomAttributes($customAttributes); $this->assertFalse($v->passes()); $v->messages()->setFormat(':message'); $this->assertEquals('Name is required!', $v->messages()->first('name')); @@ -170,7 +170,23 @@ public function testDisplayableValuesAreReplaced() ) ); $v = new Validator($trans, array('type' => '4'), array('type' => 'in:5,300')); - $v->setCustomValues($customValues); + $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')); From 8d8433ea435894cb8e43fe7596d0c815f9951bd6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=AE=89=E6=AD=A3=E8=B6=85?= Date: Wed, 23 Jul 2014 17:09:51 +0800 Subject: [PATCH 5/6] fix typo --- src/Illuminate/Validation/Validator.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Illuminate/Validation/Validator.php b/src/Illuminate/Validation/Validator.php index dfc628298fbe..598ca5430fc3 100755 --- a/src/Illuminate/Validation/Validator.php +++ b/src/Illuminate/Validation/Validator.php @@ -2128,7 +2128,7 @@ public function setAttributeNames(array $attributes) */ public function setValueNames(array $values) { - $this->customAttributes = $values; + $this->customValues = $values; return $this; } From a2296cbad9bba15713841b0bf17e68245a3d6480 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=AE=89=E6=AD=A3=E8=B6=85?= Date: Wed, 23 Jul 2014 18:00:43 +0800 Subject: [PATCH 6/6] fix comments --- tests/Validation/ValidationValidatorTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/Validation/ValidationValidatorTest.php b/tests/Validation/ValidationValidatorTest.php index f0a1d417a605..6133c77a096e 100755 --- a/tests/Validation/ValidationValidatorTest.php +++ b/tests/Validation/ValidationValidatorTest.php @@ -159,7 +159,7 @@ public function testDisplayableValuesAreReplaced() $v->messages()->setFormat(':message'); $this->assertEquals('type must be included in Short, Long.', $v->messages()->first('type')); - // set custom values by setter + // test addCustomValues $trans = $this->getRealTranslator(); $trans->addResource('array', array('validation.in' => ':attribute must be included in :values.'), 'en', 'messages'); $customValues = array(