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 1da0f5b..7adf9f3 100644 --- a/src/Bllim/Laravalid/Converter/JqueryValidation/Message.php +++ b/src/Bllim/Laravalid/Converter/JqueryValidation/Message.php @@ -1,12 +1,13 @@ $attribute]); + $message = Helper::getValidationMessage($attribute, $parsedRule['name']); return ['data-msg-ipv4' => $message]; } @@ -18,19 +19,31 @@ public function same($parsedRule, $attribute, $type) 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 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 = 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]; @@ -44,7 +57,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]; @@ -58,7 +71,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/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 7a61b77..db8d0a2 100644 --- a/src/Bllim/Laravalid/Helper.php +++ b/src/Bllim/Laravalid/Helper.php @@ -22,4 +22,36 @@ 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]); + } + + /** + * 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