From 3526224b64addb15f88f3aa0ae90933988ecd138 Mon Sep 17 00:00:00 2001 From: Jannis Pohl Date: Thu, 11 Jun 2015 15:47:50 +0200 Subject: [PATCH 1/3] moved validation message generation into a Helper method --- .../Laravalid/Converter/Base/Converter.php | 24 ++++++++----------- .../Converter/JqueryValidation/Message.php | 13 +++++----- src/Bllim/Laravalid/Helper.php | 22 +++++++++++++++++ 3 files changed, 39 insertions(+), 20 deletions(-) diff --git a/src/Bllim/Laravalid/Converter/Base/Converter.php b/src/Bllim/Laravalid/Converter/Base/Converter.php index c3c234e..9d00f3f 100644 --- a/src/Bllim/Laravalid/Converter/Base/Converter.php +++ b/src/Bllim/Laravalid/Converter/Base/Converter.php @@ -1,4 +1,7 @@ getAttributeName($attribute); - $message = \Lang::get('validation.'.$laravelRule, ['attribute' => $attribute]); + // getting user friendly validation message + $message = Helper::getValidationMessage($attribute, $laravelRule); return ['data-msg-'.$laravelRule => $message]; } - /** - * Get user friendly attribute name - * - * @return string - */ - protected function getAttributeName($attribute) - { - return !\Lang::has('validation.attributes.'.$attribute) ? $attribute : \Lang::get('validation.attributes.'.$attribute); - } - -} +} \ No newline at end of file diff --git a/src/Bllim/Laravalid/Converter/JqueryValidation/Message.php b/src/Bllim/Laravalid/Converter/JqueryValidation/Message.php index 7701dd9..00df9ad 100644 --- a/src/Bllim/Laravalid/Converter/JqueryValidation/Message.php +++ b/src/Bllim/Laravalid/Converter/JqueryValidation/Message.php @@ -1,30 +1,31 @@ $attribute]); + $message = Helper::getValidationMessage($attribute, $parsedRule['name']); return ['data-msg-ipv4' => $message]; } public function alpha($parsedRule, $attribute, $type) { - $message = Lang::get('validation.'.$parsedRule['name'], ['attribute' => $attribute]); + $message = Helper::getValidationMessage($attribute, $parsedRule['name']); return ['data-msg-regex' => $message]; } public function alphanum($parsedRule, $attribute, $type) { - $message = Lang::get('validation.'.$parsedRule['name'], ['attribute' => $attribute]); + $message = Helper::getValidationMessage($attribute, $parsedRule['name']); return ['data-msg-regex' => $message]; } public function max($parsedRule, $attribute, $type) { - $message = Lang::get('validation.'.$parsedRule['name'].'.'.$type, ['attribute' => $attribute, 'max' => $parsedRule['parameters'][0]]); + $message = Helper::getValidationMessage($attribute, $parsedRule['name'], ['max' => $parsedRule['parameters'][0]], $type); switch ($type) { case 'numeric': return ['data-msg-max' => $message]; @@ -38,7 +39,7 @@ public function max($parsedRule, $attribute, $type) public function min($parsedRule, $attribute, $type) { - $message = Lang::get('validation.'.$parsedRule['name'].'.'.$type, ['attribute' => $attribute, 'min' => $parsedRule['parameters'][0]]); + $message = Helper::getValidationMessage($attribute, $parsedRule['name'], ['min' => $parsedRule['parameters'][0]], $type); switch ($type) { case 'numeric': return ['data-msg-min' => $message]; @@ -52,7 +53,7 @@ public function min($parsedRule, $attribute, $type) public function between($parsedRule, $attribute, $type) { - $message = Lang::get('validation.'.$parsedRule['name'].'.'.$type, ['attribute' => $attribute, 'min' => $parsedRule['parameters'][0], 'max' => $parsedRule['parameters'][1]]); + $message = Helper::getValidationMessage($attribute, $parsedRule['name'], ['min' => $parsedRule['parameters'][0], 'max' => $parsedRule['parameters'][1]], $type); switch ($type) { case 'numeric': return ['data-msg-range' => $message]; diff --git a/src/Bllim/Laravalid/Helper.php b/src/Bllim/Laravalid/Helper.php index 7a61b77..984c3bd 100644 --- a/src/Bllim/Laravalid/Helper.php +++ b/src/Bllim/Laravalid/Helper.php @@ -22,4 +22,26 @@ public static function decrypt($data) return \Crypt::decrypt($data); } + /** + * Get user friendly validation message + * + * @return string + */ + public static function getValidationMessage($attribute, $rule, $data = [], $type = null) + { + $path = $rule; + if ($type !== null) + { + $path .= '.' . $type; + } + + if (\Lang::has('validation.custom.' . $attribute . '.' . $path)) + { + $path = 'custom.' . $attribute . '.' . $path; + } + + $niceName = !\Lang::has('validation.attributes.'.$attribute) ? $attribute : \Lang::get('validation.attributes.'.$attribute); + + return \Lang::get('validation.' . $path, $data + ['attribute' => $niceName]); + } } \ No newline at end of file From 7c006ea2292c63670f986edda907cdb0bbc93018 Mon Sep 17 00:00:00 2001 From: Jannis Pohl Date: Thu, 11 Jun 2015 16:09:41 +0200 Subject: [PATCH 2/3] remove array braces from validation attributes --- src/Bllim/Laravalid/FormBuilder.php | 8 ++++---- src/Bllim/Laravalid/Helper.php | 10 ++++++++++ 2 files changed, 14 insertions(+), 4 deletions(-) diff --git a/src/Bllim/Laravalid/FormBuilder.php b/src/Bllim/Laravalid/FormBuilder.php index c6cde82..cedfdbc 100644 --- a/src/Bllim/Laravalid/FormBuilder.php +++ b/src/Bllim/Laravalid/FormBuilder.php @@ -95,7 +95,7 @@ public function model($model, array $options = array(), $rules = null) */ public function input($type, $name, $value = null, $options = []) { - $options = $this->converter->convert($name) + $options; + $options = $this->converter->convert(Helper::getFormAttribute($name)) + $options; return parent::input($type, $name, $value, $options); } @@ -104,7 +104,7 @@ public function input($type, $name, $value = null, $options = []) */ public function textarea($name, $value = null, $options = []) { - $options = $this->converter->convert($name) + $options; + $options = $this->converter->convert(Helper::getFormAttribute($name)) + $options; return parent::textarea($name, $value, $options); } @@ -113,13 +113,13 @@ public function textarea($name, $value = null, $options = []) */ public function select($name, $list = [], $selected = null, $options = []) { - $options = $this->converter->convert($name) + $options; + $options = $this->converter->convert(Helper::getFormAttribute($name)) + $options; return parent::select($name, $list, $selected, $options); } protected function checkable($type, $name, $value, $checked, $options) { - $options = $this->converter->convert($name) + $options; + $options = $this->converter->convert(Helper::getFormAttribute($name)) + $options; return parent::checkable($type, $name, $value, $checked, $options); } diff --git a/src/Bllim/Laravalid/Helper.php b/src/Bllim/Laravalid/Helper.php index 984c3bd..db8d0a2 100644 --- a/src/Bllim/Laravalid/Helper.php +++ b/src/Bllim/Laravalid/Helper.php @@ -44,4 +44,14 @@ public static function getValidationMessage($attribute, $rule, $data = [], $type return \Lang::get('validation.' . $path, $data + ['attribute' => $niceName]); } + + /** + * Get the raw attribute name without array braces + * + * @return string + */ + public static function getFormAttribute($name) + { + return substr($name, -2) === '[]' ? substr($name, 0, -2) : $name; + } } \ No newline at end of file From 52df9cbba44ef7d1e2cbf3c73fbf357ae4b670b4 Mon Sep 17 00:00:00 2001 From: Jannis Pohl Date: Thu, 11 Jun 2015 16:26:17 +0200 Subject: [PATCH 3/3] add Message methods for integer/numeric as they need to convert to data-msg-number --- .../Converter/JqueryValidation/Message.php | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/src/Bllim/Laravalid/Converter/JqueryValidation/Message.php b/src/Bllim/Laravalid/Converter/JqueryValidation/Message.php index 00df9ad..53a3db0 100644 --- a/src/Bllim/Laravalid/Converter/JqueryValidation/Message.php +++ b/src/Bllim/Laravalid/Converter/JqueryValidation/Message.php @@ -22,7 +22,19 @@ public function alphanum($parsedRule, $attribute, $type) $message = Helper::getValidationMessage($attribute, $parsedRule['name']); return ['data-msg-regex' => $message]; } - + + public function integer($parsedRule, $attribute, $type) + { + $message = Helper::getValidationMessage($attribute, $parsedRule['name']); + return ['data-msg-number' => $message]; + } + + public function numeric($parsedRule, $attribute, $type) + { + $message = Helper::getValidationMessage($attribute, $parsedRule['name']); + return ['data-msg-number' => $message]; + } + public function max($parsedRule, $attribute, $type) { $message = Helper::getValidationMessage($attribute, $parsedRule['name'], ['max' => $parsedRule['parameters'][0]], $type);