This repository was archived by the owner on Mar 19, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathBarcodeValidator.php
167 lines (146 loc) · 4.92 KB
/
BarcodeValidator.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
<?php
/**
* @link http://astwell.com/
* @copyright Copyright (c) 2014 Astwell Soft
* @license http://astwell.com/license/
*/
namespace lembadm\barcode;
use Yii;
use yii\base\InvalidConfigException;
use yii\helpers\Html;
use yii\validators\Validator;
/**
* Class BarcodeValidator
*
* @package lembadm\barcode
*/
class BarcodeValidator extends Validator
{
/**
* Barcode type
*/
public $type;
/**
* Barcode type in model property
*/
public $typeAttribute;
/**
* @var string user-defined error message used when the barcode length not valid
*/
public $messageLength;
/**
* @var string user-defined error message used when the barcode contains wrong characters
*/
public $messageCharacters;
/**
* @var string user-defined error message used when the barcode checksum is wrong
*/
public $messageChecksum;
/**
* @var \yii\base\Model Barcode adapter
*/
protected $adapter;
/**
* @inheritdoc
*/
public function init()
{
if ( ! $this->type and ! $this->typeAttribute) {
throw new InvalidConfigException( 'Not set barcode type' );
}
}
/**
* @inheritdoc
*/
public function validateAttribute( $model, $attribute )
{
$this->getAdapter( $model )->validateAttribute( $model, $attribute );
}
/**
* @inheritdoc
*/
public function validateValue( $value )
{
return $this->getAdapter()->validateValue( $value );
}
/**
* @inheritdoc
*/
public function clientValidateAttribute( $model, $attribute, $view )
{
$attributeID = Html::getInputId($model, $attribute);
$typeAttributeID = Html::getInputId($model, $this->typeAttribute);
$view->registerJs(
'
$("#' . $typeAttributeID . '").on("change", function(){
var $type = $(this);
var $code = $("#' . $attributeID . '");
var $yiiActiveFormData = $code.closest("form").yiiActiveForm("data");
$.each($yiiActiveFormData.attributes, function () {
if(this.id == "' . $attributeID . '") this.status = 2;
});
});
$( document ).ready(function() {
$("#' . $typeAttributeID . '").trigger("change");
});
');
if($this->type) {
return $this->getAdapter($model)->clientValidateAttribute($model, $attribute, $view);
}
else {
BarcodeAsset::register($view);
$options = $this->getClientOptions($model, $attribute);
return 'yii.validation.barcode(value, messages, ' . json_encode($options, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE) . ');';
}
}
/**
* Returns the client side validation options.
* @param \yii\base\Model $model the model being validated
* @param string $attribute the attribute name being validated
* @return array the client side validation options
*/
public function getClientOptions($model, $attribute)
{
$label = $model->getAttributeLabel($attribute);
$format = function($message, $label){
return Yii::$app->getI18n()->format($message, ['attribute' => $label], Yii::$app->language);
};
return [
'skipOnEmpty' => $this->skipOnEmpty,
'typeAttribute' => Html::getInputId( $model, $this->typeAttribute ),
'messageLength' => $format( $this->getAdapter( $model )->messageLength, $label ),
'messageChecksum' => $format( $this->getAdapter( $model )->messageChecksum, $label ),
'messageCharacters' => $format( $this->getAdapter( $model )->messageCharacters, $label ),
];
}
/**
* @param \yii\base\Model $model
*
* @return BarcodeAbstractType
* @throws InvalidConfigException
*/
protected function getAdapter( $model = null )
{
if ( ! $this->adapter) {
$type = ( $this->type ) ? $this->type : $model->{$this->typeAttribute};
$type = ($type) ?: 'Base';
try {
$this->adapter = Yii::createObject( [
'class' => __NAMESPACE__ . '\\type\\' . $type,
'messageLength' => $this->messageLength,
'messageChecksum' => $this->messageChecksum,
'messageCharacters' => $this->messageCharacters,
] );
}
catch(\ReflectionException $e) {
$this->adapter = Yii::createObject( [
'class' => __NAMESPACE__ . '\\type\\Base',
'messageLength' => $this->messageLength,
'messageChecksum' => $this->messageChecksum,
'messageCharacters' => $this->messageCharacters,
] );
}
}
return $this->adapter;
}
}