1
|
|
|
<?php /** MicroFormModel */ |
2
|
|
|
|
3
|
|
|
namespace Micro\Form; |
4
|
|
|
|
5
|
|
|
use Micro\Validator\Validator; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Class FormModel. |
9
|
|
|
* |
10
|
|
|
* @author Oleg Lunegov <[email protected]> |
11
|
|
|
* @link https://github.com/linpax/microphp-framework |
12
|
|
|
* @copyright Copyright (c) 2013 Oleg Lunegov |
13
|
|
|
* @license https://github.com/linpax/microphp-framework/blob/master/LICENSE |
14
|
|
|
* @package Micro |
15
|
|
|
* @subpackage Form |
16
|
|
|
* @version 1.0 |
17
|
|
|
* @since 1.0 |
18
|
|
|
*/ |
19
|
|
|
abstract class FormModel implements IFormModel |
20
|
|
|
{ |
21
|
|
|
/** @var array $errors validation errors */ |
22
|
|
|
protected $errors = []; |
23
|
|
|
|
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Constructor form |
27
|
|
|
* |
28
|
|
|
* @access public |
29
|
|
|
* @result void |
30
|
|
|
*/ |
31
|
|
|
public function __construct() |
32
|
|
|
{ |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @inheritdoc |
37
|
|
|
*/ |
38
|
|
|
public function validate() |
39
|
|
|
{ |
40
|
|
|
foreach ($this->rules() AS $rule) { |
41
|
|
|
$validator = new Validator(['rule' => $rule]); |
42
|
|
|
|
43
|
|
|
if (!$validator->run($this) && (0 < count($validator->errors))) { |
44
|
|
|
$this->errors[] = $validator->errors; |
45
|
|
|
} |
46
|
|
|
} |
47
|
|
|
if (count($this->errors)) { |
48
|
|
|
return false; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
return true; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @inheritdoc |
56
|
|
|
*/ |
57
|
|
|
public function rules() |
58
|
|
|
{ |
59
|
|
|
return []; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* @inheritdoc |
64
|
|
|
*/ |
65
|
|
|
public function getClient() |
66
|
|
|
{ |
67
|
|
|
$result = 'jQuery(document).ready(function(){'; |
68
|
|
|
|
69
|
|
|
foreach ($this->rules() AS $rule) { |
70
|
|
|
$validator = new Validator(['rule' => $rule]); |
71
|
|
|
if (is_string($js = $validator->run($this, true))) { |
72
|
|
|
$result .= ' '.$js; |
73
|
|
|
} |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
return $result.'});'; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* @inheritdoc |
81
|
|
|
*/ |
82
|
|
|
public function setModelData(array $data = []) |
83
|
|
|
{ |
84
|
|
|
foreach ($data AS $key => $value) { |
85
|
|
|
$this->$key = $value; |
86
|
|
|
} |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* @inheritdoc |
91
|
|
|
*/ |
92
|
|
|
public function addError($description) |
93
|
|
|
{ |
94
|
|
|
$this->errors[] = $description; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* @inheritdoc |
99
|
|
|
*/ |
100
|
|
|
public function getErrors() |
101
|
|
|
{ |
102
|
|
|
return $this->convertMultiArrayToArray($this->errors); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* Convert results array to single array |
107
|
|
|
* |
108
|
|
|
* @access protected |
109
|
|
|
* |
110
|
|
|
* @param array $errors merge errors |
111
|
|
|
* |
112
|
|
|
* @return array |
113
|
|
|
*/ |
114
|
|
|
private function convertMultiArrayToArray($errors) |
115
|
|
|
{ |
116
|
|
|
static $result = []; |
117
|
|
|
foreach ($errors AS $error) { |
118
|
|
|
if (is_array($error)) { |
119
|
|
|
$this->convertMultiArrayToArray($error); |
120
|
|
|
} else { |
121
|
|
|
$result[] = $error; |
122
|
|
|
} |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
return $result; |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* @inheritdoc |
130
|
|
|
*/ |
131
|
|
|
public function getLabel($property) |
132
|
|
|
{ |
133
|
|
|
$elements = $this->attributeLabels(); |
134
|
|
|
|
135
|
|
|
return !empty($elements[$property]) ? $elements[$property] : $property; |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* @inheritdoc |
140
|
|
|
*/ |
141
|
|
|
public function attributeLabels() |
142
|
|
|
{ |
143
|
|
|
return []; |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* @inheritdoc |
148
|
|
|
*/ |
149
|
|
|
public function checkAttributeExists($name) |
150
|
|
|
{ |
151
|
|
|
return property_exists($this, $name); |
152
|
|
|
} |
153
|
|
|
} |
154
|
|
|
|