Commit 9f08218160d2c16c5bfb33ed1a5507cd20194e97
1 parent
b96b6861
add comma number validator
Showing
2 changed files
with
65 additions
and
3 deletions
Show diff stats
1 | +<?php | ||
2 | +/** | ||
3 | + * Created by PhpStorm. | ||
4 | + * User: Tsurkanov | ||
5 | + * Date: 25.11.2015 | ||
6 | + * Time: 13:51 | ||
7 | + */ | ||
8 | + | ||
9 | +namespace common\components; | ||
10 | + | ||
11 | +use yii\validators\NumberValidator; | ||
12 | + | ||
13 | + | ||
14 | +/** | ||
15 | + * Class CommaNumberValidator | ||
16 | + * @package common\components | ||
17 | + * validator identic to NumberValidator, | ||
18 | + * with one change - comma replaced to dot, | ||
19 | + * thus values such 21,4; 45,05 - will be valid | ||
20 | + */ | ||
21 | +class CommaNumberValidator extends NumberValidator { | ||
22 | + // override parent pattern - add ',' to it | ||
23 | + public $numberPattern = '/^\s*[-+]?[0-9]*[\.,]?[0-9]+([eE][-+]?[0-9]+)?\s*$/'; | ||
24 | + | ||
25 | + public function validateAttribute($model, $attribute) | ||
26 | + { | ||
27 | + $value = $model->$attribute; | ||
28 | + if ( !is_array($value) ) { | ||
29 | + $model->$attribute = $this->commaReplacement( $value ); | ||
30 | + $value = $model->$attribute; | ||
31 | + } | ||
32 | + | ||
33 | + if (is_array($value)) { | ||
34 | + $this->addError($model, $attribute, $this->message); | ||
35 | + return; | ||
36 | + } | ||
37 | + $pattern = $this->integerOnly ? $this->integerPattern : $this->numberPattern; | ||
38 | + if (!preg_match($pattern, "$value")) { | ||
39 | + $this->addError($model, $attribute, $this->message); | ||
40 | + } | ||
41 | + if ($this->min !== null && $value < $this->min) { | ||
42 | + $this->addError($model, $attribute, $this->tooSmall, ['min' => $this->min]); | ||
43 | + } | ||
44 | + if ($this->max !== null && $value > $this->max) { | ||
45 | + $this->addError($model, $attribute, $this->tooBig, ['max' => $this->max]); | ||
46 | + } | ||
47 | + } | ||
48 | + | ||
49 | + protected function validateValue($value) | ||
50 | + { | ||
51 | + $value = $this->commaReplacement( $value ); | ||
52 | + | ||
53 | + return parent::validateValue( $value ); | ||
54 | + } | ||
55 | + | ||
56 | + | ||
57 | + protected function commaReplacement( $value ) | ||
58 | + { | ||
59 | + return str_replace( ',', '.', $value ); | ||
60 | + } | ||
61 | +} | ||
0 | \ No newline at end of file | 62 | \ No newline at end of file |
common/models/Currency.php
@@ -30,9 +30,10 @@ class Currency extends \yii\db\ActiveRecord | @@ -30,9 +30,10 @@ class Currency extends \yii\db\ActiveRecord | ||
30 | { | 30 | { |
31 | return [ | 31 | return [ |
32 | [['name', 'rate'], 'required'], | 32 | [['name', 'rate'], 'required'], |
33 | - [['rate'], 'filter','filter' => function($value){ | ||
34 | - return (float) str_replace( ',', '.', $value );} | ||
35 | - ], | 33 | +// [['rate'], 'filter','filter' => function($value){ |
34 | +// return (float) str_replace( ',', '.', $value );} | ||
35 | +// ], | ||
36 | + ['rate', \common\components\CommaNumberValidator::className()], | ||
36 | [['is_default'], 'integer'], | 37 | [['is_default'], 'integer'], |
37 | [['timestamp'], 'safe'], | 38 | [['timestamp'], 'safe'], |
38 | [['name'], 'string', 'max' => 50], | 39 | [['name'], 'string', 'max' => 50], |