1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* TForm.php |
4
|
|
|
* |
5
|
|
|
* @copyright More in license.md |
6
|
|
|
* @license https://www.ipublikuj.eu |
7
|
|
|
* @author Adam Kadlec <[email protected]> |
8
|
|
|
* @package iPublikuj:Forms! |
9
|
|
|
* @subpackage Forms |
10
|
|
|
* @since 1.0.0 |
11
|
|
|
* |
12
|
|
|
* @date 26.05.13 |
13
|
|
|
*/ |
14
|
|
|
|
15
|
|
|
namespace IPub\Forms\Forms; |
16
|
|
|
|
17
|
|
|
use Nette; |
18
|
|
|
use Nette\Forms\Controls; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Form trait for decorating form |
22
|
|
|
* |
23
|
|
|
* @package iPublikuj:Forms! |
24
|
|
|
* @subpackage Forms |
25
|
|
|
* |
26
|
|
|
* @author Adam Kadlec <[email protected]> |
27
|
|
|
*/ |
28
|
|
|
trait TForm |
29
|
|
|
{ |
30
|
|
|
/** |
31
|
|
|
* @var mixed |
32
|
|
|
*/ |
33
|
|
|
protected $id; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @var string|NULL |
37
|
|
|
*/ |
38
|
|
|
protected $errorClass = NULL; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @param array $defaults |
42
|
|
|
* |
43
|
|
|
* @return void |
44
|
|
|
*/ |
45
|
|
|
public function restore(array $defaults = []) : void |
46
|
|
|
{ |
47
|
|
|
$this->setDefaults($defaults, TRUE); |
|
|
|
|
48
|
|
|
$this->setValues($defaults, TRUE); |
|
|
|
|
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @return mixed |
53
|
|
|
*/ |
54
|
|
|
public function getId() |
55
|
|
|
{ |
56
|
|
|
return $this->id; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @param mixed $id |
61
|
|
|
* |
62
|
|
|
* @return void |
63
|
|
|
*/ |
64
|
|
|
public function setId($id) : void |
65
|
|
|
{ |
66
|
|
|
$this->id = $id; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* @param string $errorClass |
71
|
|
|
* |
72
|
|
|
* @return void |
73
|
|
|
*/ |
74
|
|
|
public function setErrorClass(string $errorClass) : void |
75
|
|
|
{ |
76
|
|
|
$this->errorClass = $errorClass; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* @return void |
81
|
|
|
*/ |
82
|
|
|
protected function beforeRender() : void |
83
|
|
|
{ |
84
|
|
|
parent::beforeRender(); |
85
|
|
|
|
86
|
|
|
/** @var Controls\BaseControl $control */ |
87
|
|
|
foreach ($this->getControls() as $control) { |
|
|
|
|
88
|
|
|
$inputClass = []; |
89
|
|
|
|
90
|
|
|
if ($control->isRequired()) { |
91
|
|
|
$control->getLabelPrototype()->appendAttribute('required', 'required'); |
92
|
|
|
$control->getLabelPrototype()->appendAttribute('class', 'ipub-field-required'); |
93
|
|
|
|
94
|
|
|
$inputClass[] = 'ipub-field-required'; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
if ($control->hasErrors()) { |
98
|
|
|
$inputClass[] = 'ipub-field-error'; |
99
|
|
|
|
100
|
|
|
if ($this->errorClass) { |
101
|
|
|
$inputClass[] = $this->errorClass; |
102
|
|
|
} |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
$control->getControlPrototype()->addAttributes(['class' => $inputClass]); |
106
|
|
|
} |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* @param string $name |
111
|
|
|
* @param string $class |
112
|
|
|
* |
113
|
|
|
* @return void |
114
|
|
|
*/ |
115
|
|
|
protected function addExtension(string $name, string $class) : void |
116
|
|
|
{ |
117
|
|
|
Nette\Forms\Container::extensionMethod($name, function (Nette\Forms\Container $container, $name, $label = NULL) use ($class) { |
118
|
|
|
return $container[$name] = new $class($label); |
119
|
|
|
}); |
120
|
|
|
} |
121
|
|
|
} |
122
|
|
|
|
This check looks for methods that are used by a trait but not required by it.
To illustrate, let’s look at the following code example
The trait
Idable
provides a methodequalsId
that in turn relies on the methodgetId()
. If this method does not exist on a class mixing in this trait, the method will fail.Adding the
getId()
as an abstract method to the trait will make sure it is available.