From eaa2cf593c60cae32dfe53da6cfe05a74060acb6 Mon Sep 17 00:00:00 2001 From: Taylor Otwell Date: Wed, 1 Feb 2012 10:11:27 -0600 Subject: [PATCH] added support for custom validation lines. --- application/language/en/validation.php | 51 +++++++++++++++++--------- laravel/validator.php | 10 ++++- 2 files changed, 42 insertions(+), 19 deletions(-) diff --git a/application/language/en/validation.php b/application/language/en/validation.php index adde4e03..7e46e155 100644 --- a/application/language/en/validation.php +++ b/application/language/en/validation.php @@ -2,23 +2,6 @@ return array( - /* - |-------------------------------------------------------------------------- - | Validation Attribute Language Lines - |-------------------------------------------------------------------------- - | - | The following language lines are used to swap attribute place-holders - | with something more reader friendly such as "E-Mail Address" instead - | of "email". Your users will thank you. - | - | The Validator class will automatically search this array of lines it - | is attempting to replace the :attribute place-holder in messages. - | It's pretty slick. We think you'll like it. - | - */ - - 'attributes' => array(), - /* |-------------------------------------------------------------------------- | Validation Language Lines @@ -74,4 +57,38 @@ "unique" => "The :attribute has already been taken.", "url" => "The :attribute format is invalid.", + /* + |-------------------------------------------------------------------------- + | Custom Validation Language Lines + |-------------------------------------------------------------------------- + | + | Here you may specify custom validation messages for attributes using the + | convention "attribute_rule" to name the lines. This helps keep your + | custom validation clean and tidy. + | + | So, say you want to use a custom validation message when validating that + | the "email" attribute is unique. Just add "email_unique" to this array + | with your custom message. The Validator will handle the rest! + | + */ + + 'custom' => array(), + + /* + |-------------------------------------------------------------------------- + | Validation Attributes + |-------------------------------------------------------------------------- + | + | The following language lines are used to swap attribute place-holders + | with something more reader friendly such as "E-Mail Address" instead + | of "email". Your users will thank you. + | + | The Validator class will automatically search this array of lines it + | is attempting to replace the :attribute place-holder in messages. + | It's pretty slick. We think you'll like it. + | + */ + + 'attributes' => array(), + ); \ No newline at end of file diff --git a/laravel/validator.php b/laravel/validator.php index 7937ec38..73e1b205 100644 --- a/laravel/validator.php +++ b/laravel/validator.php @@ -604,9 +604,15 @@ protected function message($attribute, $rule) // First we'll check for developer specified, attribute specific messages. // These messages take first priority. They allow the fine-grained tuning // of error messages for each rule. - if (array_key_exists($attribute.'_'.$rule, $this->messages)) + $custom = $attribute.'_'.$rule; + + if (array_key_exists($custom, $this->messages)) { - return $this->messages[$attribute.'_'.$rule]; + return $this->messages[$custom]; + } + elseif (Lang::has($custom = "validation.custom.{$custom}", $this->language)) + { + return Lang::line($custom)->get($this->language); } // Next we'll check for developer specified, rule specific error messages.