-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBoundField.php
142 lines (109 loc) · 3.53 KB
/
BoundField.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
<?php
namespace PHPForm\Bounds;
use PHPForm\Config;
class BoundField
{
const TEMPLATE_LABEL = "label.html";
private $form;
private $field;
private $name;
private $bound_widgets_cache;
public $html_name;
public $help_text;
public $label;
public function __construct($form, $field, $name)
{
$this->form = $form;
$this->field = $field;
$this->name = $name;
$this->html_name = $form->addPrefix($name);
$this->label = $field->getLabel($name);
$this->help_text = $field->getHelpText();
}
public function __toString()
{
return $this->asWidget();
}
public function __get($name)
{
if ($name == 'errors') {
return $this->form->getFieldErrors($this->name);
}
if ($name == 'has_errors') {
return $this->form->hasErrors($this->name);
}
if ($name == 'label_tag') {
return $this->labelTag();
}
if ($name == 'is_required') {
return $this->field->isRequired();
}
if ($name == 'value') {
return $this->getValue();
}
if ($name == 'options') {
if (!isset($bound_widgets_cache)) {
$bound_widgets_cache = $this->getSubWidgets();
}
return $bound_widgets_cache;
}
}
private function getSubWidgets(array $attrs = array())
{
$bounds = [];
$attrs = $this->buildWidgetAttrs($attrs);
$options = $this->field->getWidget()->getOptions($this->html_name, $this->getValue(), $attrs);
foreach ($options as $option) {
$bounds[] = new BoundWidget($option);
}
return $bounds;
}
protected function asWidget($widget = null, array $attrs = array())
{
$widget = is_null($widget) ? $this->field->getWidget() : $widget;
$attrs = $this->buildWidgetAttrs($attrs);
return $widget->render($this->html_name, $this->getValue(), $this->label, $attrs);
}
public function labelTag($contents = null, array $attrs = array())
{
$contents = is_null($contents) ? $this->label : $contents;
if (empty($contents)) {
return "";
}
$widget = $this->field->getWidget();
$renderer = Config::getInstance()->getRenderer();
return $renderer->render(self::TEMPLATE_LABEL, array(
"for" => $widget->buildAutoId($this->html_name),
"attrs" => $attrs,
"contents" => $contents,
"required" => $this->field->isRequired()
));
}
protected function getValue()
{
if ($this->form->isBound() && !$this->field->isDisabled()) {
$widget = $this->field->getWidget();
$value = $widget->valueFromData($this->form->getData(), $this->form->getFiles(), $this->html_name);
} else {
$value = $this->form->getInitialForField($this->field, $this->name);
}
return $value;
}
private function buildWidgetAttrs(array $attrs = array())
{
$css_classes = $this->form->getCssClasses();
if ($this->has_errors) {
$css_classes[] = $this->form->getErrorCssClass();
}
if (!empty($css_classes)) {
$attrs['class'] = implode(" ", $css_classes);
}
if ($this->field->isRequired()) {
$attrs['required'] = 'required';
}
if ($this->field->isDisabled()) {
$attrs['disabled'] = 'disabled';
}
return $attrs;
}
}