From e01ac2d9575e7881eae3e4077c30537e026964df Mon Sep 17 00:00:00 2001 From: Andy Higgs Date: Mon, 14 Jan 2013 00:07:09 +0000 Subject: [PATCH 1/2] Exposed :key (in addition to :message) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit I have a use case where I could do with :key exposed. If I echo out my errors, on some occasions I output the errors in a single block then treat them using JS (usually moving them to an element on the page by the ID stored in a data attribute). If I want to print
  • :message
  • , I cannot currently access the :key part (where :key is the ID of the associated field). Being able to do this (as adjusted) allows a user to identify exactly what field an individual message was associated with e.g. when using ->all(), and is a simple exposure of data already held. --- src/Illuminate/Support/MessageBag.php | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/src/Illuminate/Support/MessageBag.php b/src/Illuminate/Support/MessageBag.php index 064fdddfa645..3e241a3a6046 100644 --- a/src/Illuminate/Support/MessageBag.php +++ b/src/Illuminate/Support/MessageBag.php @@ -89,7 +89,7 @@ public function get($key, $format = null) // methods is to return back an array of messages in the first place. if (array_key_exists($key, $this->messages)) { - return $this->transform($this->messages[$key], $format); + return $this->transform($this->messages[$key], $format, $key); } return array(); @@ -107,9 +107,9 @@ public function all($format = null) $all = array(); - foreach ($this->messages as $messages) + foreach ($this->messages as $key => $messages) { - $all = array_merge($all, $this->transform($messages, $format)); + $all = array_merge($all, $this->transform($messages, $format, $key)); } return $all; @@ -122,16 +122,17 @@ public function all($format = null) * @param string $format * @return array */ - protected function transform($messages, $format) + protected function transform($messages, $format, $messages_key = null) { $messages = (array) $messages; // We will simply spin through the given messages and transform each one - // replacing the :message place holder with the real message allowing + // replacing the :message place holder with the real message + // and replacing the :key place holder with the message array key // the messages to be easily formatted to each developer's desires. foreach ($messages as $key => &$message) { - $message = str_replace(':message', $message, $format); + $message = str_replace(array(':message',':key'), array($message,$messages_key), $format); } return $messages; @@ -198,4 +199,4 @@ public function count() return count($this->messages); } -} \ No newline at end of file +} From 6afdd7664b3f78dcd0db72f1930c182170878a42 Mon Sep 17 00:00:00 2001 From: Andy Higgs Date: Mon, 14 Jan 2013 00:18:10 +0000 Subject: [PATCH 2/2] Coding standards update to original patch MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Updates following comments by franzliedke --- src/Illuminate/Support/MessageBag.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/Illuminate/Support/MessageBag.php b/src/Illuminate/Support/MessageBag.php index 3e241a3a6046..1af5e2af6dad 100644 --- a/src/Illuminate/Support/MessageBag.php +++ b/src/Illuminate/Support/MessageBag.php @@ -122,7 +122,7 @@ public function all($format = null) * @param string $format * @return array */ - protected function transform($messages, $format, $messages_key = null) + protected function transform($messages, $format, $messagesKey = null) { $messages = (array) $messages; @@ -132,7 +132,7 @@ protected function transform($messages, $format, $messages_key = null) // the messages to be easily formatted to each developer's desires. foreach ($messages as $key => &$message) { - $message = str_replace(array(':message',':key'), array($message,$messages_key), $format); + $message = str_replace(array(':message', ':key'), array($message, $messagesKey), $format); } return $messages; @@ -199,4 +199,4 @@ public function count() return count($this->messages); } -} +}