From 19cd539586450788410f42e09e848b0fa488272a Mon Sep 17 00:00:00 2001 From: Jason Walton Date: Wed, 19 Dec 2012 12:05:51 -0700 Subject: [PATCH] added validation message to language file for required_with Signed-off-by: Jason Walton --- application/language/en/validation.php | 3 ++- .../application/language/en/validation.php | 3 ++- laravel/tests/cases/validator.test.php | 22 ++++++++++++++++++- laravel/validator.php | 16 +++++++++++++- 4 files changed, 40 insertions(+), 4 deletions(-) diff --git a/application/language/en/validation.php b/application/language/en/validation.php index e5116996..74f90264 100644 --- a/application/language/en/validation.php +++ b/application/language/en/validation.php @@ -58,6 +58,7 @@ "not_in" => "The selected :attribute is invalid.", "numeric" => "The :attribute must be a number.", "required" => "The :attribute field is required.", + "required_with" => "The :attribute field is required with :field", "same" => "The :attribute and :other must match.", "size" => array( "numeric" => "The :attribute must be :size.", @@ -101,4 +102,4 @@ 'attributes' => array(), -); \ No newline at end of file +); diff --git a/laravel/tests/application/language/en/validation.php b/laravel/tests/application/language/en/validation.php index 5c6354dd..9caa1fc4 100644 --- a/laravel/tests/application/language/en/validation.php +++ b/laravel/tests/application/language/en/validation.php @@ -50,6 +50,7 @@ "not_in" => "The selected :attribute is invalid.", "numeric" => "The :attribute must be a number.", "required" => "The :attribute field is required.", + "required_with" => "The :attribute field is required with :field", "same" => "The :attribute and :other must match.", "size" => array( "numeric" => "The :attribute must be :size.", @@ -93,4 +94,4 @@ 'attributes' => array('test_attribute' => 'attribute'), -); \ No newline at end of file +); diff --git a/laravel/tests/cases/validator.test.php b/laravel/tests/cases/validator.test.php index bde1cea1..de8fe10f 100644 --- a/laravel/tests/cases/validator.test.php +++ b/laravel/tests/cases/validator.test.php @@ -666,4 +666,24 @@ public function testCustomAttributesAreReplaced() $this->assertEquals($expect, $v->errors->first('test_attribute')); } -} \ No newline at end of file + /** + * Test required_with attribute names are replaced. + * + * @group laravel + */ + public function testRequiredWithAttributesAreReplaced() + { + $lang = require path('app').'language/en/validation.php'; + + $data = array('first_name' => 'Taylor', 'last_name' => ''); + + $rules = array('first_name' => 'required', 'last_name' => 'required_with:first_name'); + + $v = Validator::make($data, $rules); + $v->valid(); + + $expect = str_replace(array(':attribute', ':field'), array('last name', 'first name'), $lang['required_with']); + $this->assertEquals($expect, $v->errors->first('last_name')); + } + +} diff --git a/laravel/validator.php b/laravel/validator.php index ab446860..12c92912 100644 --- a/laravel/validator.php +++ b/laravel/validator.php @@ -863,6 +863,20 @@ protected function replace($message, $attribute, $rule, $parameters) return $message; } + /** + * Replace all place-holders for the required_with rule. + * + * @param string $message + * @param string $attribute + * @param string $rule + * @param array $parameters + * @return string + */ + protected function replace_required_with($message, $attribute, $rule, $parameters) + { + return str_replace(':field', $this->attribute($parameters[0]), $message); + } + /** * Replace all place-holders for the between rule. * @@ -1208,4 +1222,4 @@ public function __call($method, $parameters) throw new \Exception("Method [$method] does not exist."); } -} \ No newline at end of file +}