1
|
|
|
<?php /** MicroCompareValidator */ |
2
|
|
|
|
3
|
|
|
namespace Micro\Validator; |
4
|
|
|
|
5
|
|
|
use Micro\Base\Exception; |
6
|
|
|
use Micro\Form\IFormModel; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* CompareValidator class file. |
10
|
|
|
* |
11
|
|
|
* @author Oleg Lunegov <[email protected]> |
12
|
|
|
* @link https://github.com/lugnsk/micro |
13
|
|
|
* @copyright Copyright © 2013 Oleg Lunegov |
14
|
|
|
* @license /LICENSE |
15
|
|
|
* @package Micro |
16
|
|
|
* @subpackage Validator |
17
|
|
|
* @version 1.0 |
18
|
|
|
* @since 1.0 |
19
|
|
|
*/ |
20
|
|
|
class CompareValidator extends BaseValidator |
21
|
|
|
{ |
22
|
|
|
/** |
23
|
|
|
* @inheritdoc |
24
|
|
|
* @throws Exception |
25
|
|
|
*/ |
26
|
|
|
public function validate(IFormModel $model) |
27
|
|
|
{ |
28
|
|
|
if (empty($this->params['attribute']) && empty($this->params['value'])) { |
29
|
|
|
return false; |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
if (!$model->checkAttributeExists($this->params['attribute'])) { |
33
|
|
|
throw new Exception('Attribute `' . $this->params['attribute'] . '` not found into ' . get_class($model)); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
foreach ($this->elements AS $element) { |
37
|
|
|
if (!$model->checkAttributeExists($element)) { |
38
|
|
|
$this->errors[] = 'Parameter ' . $element . ' not defined in class ' . get_class($model); |
39
|
|
|
|
40
|
|
|
return false; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
$elementValue = $model->$element; |
44
|
|
|
if (!empty($this->params['value']) && ($this->params['value'] !== $elementValue)) { |
45
|
|
|
$this->errors[] = 'Parameter ' . $element . ' not equal ' . $this->params['value']; |
46
|
|
|
|
47
|
|
|
return false; |
48
|
|
|
} elseif (!empty($this->params['attribute']) && ($model->{$this->params['attribute']} !== $elementValue)) { |
49
|
|
|
$this->errors[] = 'Parameter ' . $element . ' not equal ' . $model->{$this->params['attribute']}; |
50
|
|
|
|
51
|
|
|
return false; |
52
|
|
|
} |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
return true; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* @inheritdoc |
60
|
|
|
*/ |
61
|
|
|
public function client(IFormModel $model) |
62
|
|
|
{ |
63
|
|
|
$value = $this->params['value']; |
64
|
|
|
if (!$value) { |
65
|
|
|
$attribute = $this->params['attribute']; |
66
|
|
|
$value = $model->$attribute; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
$js = 'if (this.value!="' . $value . '") { e.preventDefault(); this.focus(); alert(\'Value is not compatible\'); }'; |
70
|
|
|
|
71
|
|
|
return $js; |
72
|
|
|
} |
73
|
|
|
} |
74
|
|
|
|