1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Nip\Form\Elements; |
4
|
|
|
|
5
|
|
|
use Nip\Form\AbstractForm; |
6
|
|
|
use Nip\Form\Elements\Traits\HasAttributesTrait; |
7
|
|
|
use Nip\Form\Elements\Traits\HasDecoratorsTrait; |
8
|
|
|
use Nip\Form\Elements\Traits\HasOptionsTrait; |
9
|
|
|
use Nip\Form\Elements\Traits\HasRendererTrait; |
10
|
|
|
use Nip\Form\Elements\Traits\HasUniqueIdTrait; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Class AbstractElement |
14
|
|
|
* @package Nip\Form\Elements |
15
|
|
|
*/ |
16
|
|
|
abstract class AbstractElement implements ElementInterface |
17
|
|
|
{ |
18
|
|
|
use HasUniqueIdTrait; |
19
|
|
|
use HasAttributesTrait; |
20
|
|
|
use HasOptionsTrait; |
21
|
|
|
use HasDecoratorsTrait; |
22
|
|
|
use HasRendererTrait; |
23
|
|
|
|
24
|
|
|
protected $_form; |
25
|
|
|
|
26
|
|
|
protected $_isRequired; |
27
|
|
|
protected $_errors = []; |
28
|
|
|
protected $_policies; |
29
|
|
|
|
30
|
2 |
|
public function __construct($form) |
31
|
|
|
{ |
32
|
2 |
|
$this->setForm($form); |
33
|
2 |
|
$this->init(); |
34
|
2 |
|
} |
35
|
|
|
|
36
|
2 |
|
public function init() |
37
|
|
|
{ |
38
|
2 |
|
} |
39
|
|
|
|
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @return AbstractForm |
43
|
|
|
*/ |
44
|
2 |
|
public function getForm() |
45
|
|
|
{ |
46
|
2 |
|
return $this->_form; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @param AbstractForm $form |
51
|
|
|
* @return $this |
52
|
|
|
*/ |
53
|
2 |
|
public function setForm(AbstractForm $form) |
54
|
|
|
{ |
55
|
2 |
|
$this->_form = $form; |
56
|
|
|
|
57
|
2 |
|
return $this; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @param $data |
62
|
|
|
* @param string $source |
63
|
|
|
* @return Nip_Form_Element_Abstract |
64
|
|
|
*/ |
65
|
|
|
public function getData($data, $source = 'abstract') |
66
|
|
|
{ |
67
|
|
|
if ($source == 'model') { |
68
|
|
|
return $this->getDataFromModel($data); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
return $this->getDataFromRequest($data); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* @param $data |
76
|
|
|
* @return $this |
77
|
|
|
*/ |
78
|
|
|
public function getDataFromModel($data) |
79
|
|
|
{ |
80
|
|
|
$this->setValue($data); |
81
|
|
|
|
82
|
|
|
return $this; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* @param $request |
87
|
|
|
* @return $this |
88
|
|
|
*/ |
89
|
|
|
public function getDataFromRequest($request) |
90
|
|
|
{ |
91
|
|
|
$request = clean($request); |
92
|
|
|
$this->setValue($request); |
93
|
|
|
|
94
|
|
|
return $this; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* @param boolean $isRequired |
99
|
|
|
* @return $this |
100
|
|
|
*/ |
101
|
1 |
|
public function setRequired($isRequired) |
102
|
|
|
{ |
103
|
1 |
|
$this->_isRequired = (bool)$isRequired; |
104
|
|
|
|
105
|
1 |
|
return $this; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
public function validate() |
109
|
|
|
{ |
110
|
|
|
if ($this->isRequired() && !$this->getValue()) { |
111
|
|
|
$message = $this->getForm()->getMessageTemplate('no-'.$this->getName()); |
112
|
|
|
if (!$message) { |
113
|
|
|
$translateSlug = 'general.form.errors.required'; |
114
|
|
|
$message = app('translator')->translate($translateSlug, ['label' => $this->getLabel()]); |
115
|
|
|
if ($message == $translateSlug) { |
116
|
|
|
$message = $message ? $message : 'The field `'.$this->getLabel().'` is mandatory.'; |
117
|
|
|
} |
118
|
|
|
} |
119
|
|
|
$this->addError($message); |
120
|
|
|
} |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* @return bool |
125
|
|
|
*/ |
126
|
|
|
public function isRequired() |
127
|
|
|
{ |
128
|
|
|
return (bool)$this->_isRequired; |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* @param $message |
133
|
|
|
* @return $this |
134
|
|
|
*/ |
135
|
|
|
public function addError($message) |
136
|
|
|
{ |
137
|
|
|
$this->_errors[] = $message; |
138
|
|
|
|
139
|
|
|
return $this; |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* @return array |
144
|
|
|
*/ |
145
|
|
|
public function getErrors() |
146
|
|
|
{ |
147
|
|
|
return $this->_errors; |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* @return bool |
152
|
|
|
*/ |
153
|
|
|
public function isError() |
154
|
|
|
{ |
155
|
|
|
return count($this->_errors) > 0; |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
/** |
159
|
|
|
* @return bool |
160
|
|
|
*/ |
161
|
|
|
public function isGroup() |
162
|
|
|
{ |
163
|
|
|
return false; |
164
|
|
|
} |
165
|
|
|
} |
166
|
|
|
|