Skip to content
This repository was archived by the owner on Jul 16, 2023. It is now read-only.

Commit 2ef1adb

Browse files
committed
Merge pull request bgultekin#11 from QN-Solutions/better-translation
moved validation message generation into a Helper method
2 parents 9bf1793 + 52df9cb commit 2ef1adb

File tree

4 files changed

+66
-25
lines changed

4 files changed

+66
-25
lines changed

src/Bllim/Laravalid/Converter/Base/Converter.php

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
<?php namespace Bllim\Laravalid\Converter\Base;
2+
3+
use Bllim\Laravalid\Helper;
4+
25
/**
36
* Base converter class for converter plugins
47
*
@@ -173,6 +176,10 @@ protected function getTypeOfInput($rulesOfInput)
173176
{
174177
return 'numeric';
175178
}
179+
elseif ($parsedRule['name'] === 'array')
180+
{
181+
return 'array';
182+
}
176183
}
177184

178185
return 'string';
@@ -201,21 +208,10 @@ protected function parseValidationRule($rule)
201208
*/
202209
protected function getDefaultErrorMessage($laravelRule, $attribute)
203210
{
204-
// getting user friendly attribute name
205-
$attribute = $this->getAttributeName($attribute);
206-
$message = \Lang::get('validation.'.$laravelRule, ['attribute' => $attribute]);
211+
// getting user friendly validation message
212+
$message = Helper::getValidationMessage($attribute, $laravelRule);
207213

208214
return ['data-msg-'.$laravelRule => $message];
209215
}
210216

211-
/**
212-
* Get user friendly attribute name
213-
*
214-
* @return string
215-
*/
216-
protected function getAttributeName($attribute)
217-
{
218-
return !\Lang::has('validation.attributes.'.$attribute) ? $attribute : \Lang::get('validation.attributes.'.$attribute);
219-
}
220-
221-
}
217+
}

src/Bllim/Laravalid/Converter/JqueryValidation/Message.php

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
<?php namespace Bllim\Laravalid\Converter\JqueryValidation;
22

33
use Lang;
4+
use Bllim\Laravalid\Helper;
45

56
class Message extends \Bllim\Laravalid\Converter\Base\Message {
67

78
public function ip($parsedRule, $attribute, $type)
89
{
9-
$message = Lang::get('validation.'.$parsedRule['name'], ['attribute' => $attribute]);
10+
$message = Helper::getValidationMessage($attribute, $parsedRule['name']);
1011
return ['data-msg-ipv4' => $message];
1112
}
1213

@@ -18,19 +19,31 @@ public function same($parsedRule, $attribute, $type)
1819

1920
public function alpha($parsedRule, $attribute, $type)
2021
{
21-
$message = Lang::get('validation.'.$parsedRule['name'], ['attribute' => $attribute]);
22+
$message = Helper::getValidationMessage($attribute, $parsedRule['name']);
2223
return ['data-msg-regex' => $message];
2324
}
2425

2526
public function alphanum($parsedRule, $attribute, $type)
2627
{
27-
$message = Lang::get('validation.'.$parsedRule['name'], ['attribute' => $attribute]);
28+
$message = Helper::getValidationMessage($attribute, $parsedRule['name']);
2829
return ['data-msg-regex' => $message];
2930
}
30-
31+
32+
public function integer($parsedRule, $attribute, $type)
33+
{
34+
$message = Helper::getValidationMessage($attribute, $parsedRule['name']);
35+
return ['data-msg-number' => $message];
36+
}
37+
38+
public function numeric($parsedRule, $attribute, $type)
39+
{
40+
$message = Helper::getValidationMessage($attribute, $parsedRule['name']);
41+
return ['data-msg-number' => $message];
42+
}
43+
3144
public function max($parsedRule, $attribute, $type)
3245
{
33-
$message = Lang::get('validation.'.$parsedRule['name'].'.'.$type, ['attribute' => $attribute, 'max' => $parsedRule['parameters'][0]]);
46+
$message = Helper::getValidationMessage($attribute, $parsedRule['name'], ['max' => $parsedRule['parameters'][0]], $type);
3447
switch ($type) {
3548
case 'numeric':
3649
return ['data-msg-max' => $message];
@@ -44,7 +57,7 @@ public function max($parsedRule, $attribute, $type)
4457

4558
public function min($parsedRule, $attribute, $type)
4659
{
47-
$message = Lang::get('validation.'.$parsedRule['name'].'.'.$type, ['attribute' => $attribute, 'min' => $parsedRule['parameters'][0]]);
60+
$message = Helper::getValidationMessage($attribute, $parsedRule['name'], ['min' => $parsedRule['parameters'][0]], $type);
4861
switch ($type) {
4962
case 'numeric':
5063
return ['data-msg-min' => $message];
@@ -58,7 +71,7 @@ public function min($parsedRule, $attribute, $type)
5871

5972
public function between($parsedRule, $attribute, $type)
6073
{
61-
$message = Lang::get('validation.'.$parsedRule['name'].'.'.$type, ['attribute' => $attribute, 'min' => $parsedRule['parameters'][0], 'max' => $parsedRule['parameters'][1]]);
74+
$message = Helper::getValidationMessage($attribute, $parsedRule['name'], ['min' => $parsedRule['parameters'][0], 'max' => $parsedRule['parameters'][1]], $type);
6275
switch ($type) {
6376
case 'numeric':
6477
return ['data-msg-range' => $message];

src/Bllim/Laravalid/FormBuilder.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ public function model($model, array $options = array(), $rules = null)
9595
*/
9696
public function input($type, $name, $value = null, $options = [])
9797
{
98-
$options = $this->converter->convert($name) + $options;
98+
$options = $this->converter->convert(Helper::getFormAttribute($name)) + $options;
9999
return parent::input($type, $name, $value, $options);
100100
}
101101

@@ -104,7 +104,7 @@ public function input($type, $name, $value = null, $options = [])
104104
*/
105105
public function textarea($name, $value = null, $options = [])
106106
{
107-
$options = $this->converter->convert($name) + $options;
107+
$options = $this->converter->convert(Helper::getFormAttribute($name)) + $options;
108108
return parent::textarea($name, $value, $options);
109109
}
110110

@@ -113,13 +113,13 @@ public function textarea($name, $value = null, $options = [])
113113
*/
114114
public function select($name, $list = [], $selected = null, $options = [])
115115
{
116-
$options = $this->converter->convert($name) + $options;
116+
$options = $this->converter->convert(Helper::getFormAttribute($name)) + $options;
117117
return parent::select($name, $list, $selected, $options);
118118
}
119119

120120
protected function checkable($type, $name, $value, $checked, $options)
121121
{
122-
$options = $this->converter->convert($name) + $options;
122+
$options = $this->converter->convert(Helper::getFormAttribute($name)) + $options;
123123
return parent::checkable($type, $name, $value, $checked, $options);
124124
}
125125

src/Bllim/Laravalid/Helper.php

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,36 @@ public static function decrypt($data)
2222
return \Crypt::decrypt($data);
2323
}
2424

25+
/**
26+
* Get user friendly validation message
27+
*
28+
* @return string
29+
*/
30+
public static function getValidationMessage($attribute, $rule, $data = [], $type = null)
31+
{
32+
$path = $rule;
33+
if ($type !== null)
34+
{
35+
$path .= '.' . $type;
36+
}
37+
38+
if (\Lang::has('validation.custom.' . $attribute . '.' . $path))
39+
{
40+
$path = 'custom.' . $attribute . '.' . $path;
41+
}
42+
43+
$niceName = !\Lang::has('validation.attributes.'.$attribute) ? $attribute : \Lang::get('validation.attributes.'.$attribute);
44+
45+
return \Lang::get('validation.' . $path, $data + ['attribute' => $niceName]);
46+
}
47+
48+
/**
49+
* Get the raw attribute name without array braces
50+
*
51+
* @return string
52+
*/
53+
public static function getFormAttribute($name)
54+
{
55+
return substr($name, -2) === '[]' ? substr($name, 0, -2) : $name;
56+
}
2557
}

0 commit comments

Comments
 (0)