Skip to content

Commit

Permalink
Merge pull request #11 from QN-Solutions/better-translation
Browse files Browse the repository at this point in the history
moved validation message generation into a Helper method
  • Loading branch information
bllim committed Jul 22, 2015
2 parents 9bf1793 + 52df9cb commit 2ef1adb
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 25 deletions.
24 changes: 10 additions & 14 deletions src/Bllim/Laravalid/Converter/Base/Converter.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
<?php namespace Bllim\Laravalid\Converter\Base;

use Bllim\Laravalid\Helper;

/**
* Base converter class for converter plugins
*
Expand Down Expand Up @@ -173,6 +176,10 @@ protected function getTypeOfInput($rulesOfInput)
{
return 'numeric';
}
elseif ($parsedRule['name'] === 'array')
{
return 'array';
}
}

return 'string';
Expand Down Expand Up @@ -201,21 +208,10 @@ protected function parseValidationRule($rule)
*/
protected function getDefaultErrorMessage($laravelRule, $attribute)
{
// getting user friendly attribute name
$attribute = $this->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);
}

}
}
27 changes: 20 additions & 7 deletions src/Bllim/Laravalid/Converter/JqueryValidation/Message.php
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
<?php namespace Bllim\Laravalid\Converter\JqueryValidation;

use Lang;
use Bllim\Laravalid\Helper;

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

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

Expand All @@ -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];
Expand All @@ -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];
Expand All @@ -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];
Expand Down
8 changes: 4 additions & 4 deletions src/Bllim/Laravalid/FormBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand All @@ -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);
}

Expand All @@ -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);
}

Expand Down
32 changes: 32 additions & 0 deletions src/Bllim/Laravalid/Helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}

0 comments on commit 2ef1adb

Please sign in to comment.