Skip to content

Commit c3b4b73

Browse files
committed
Improve form handling for arrays of elements
1 parent 1d9cd5e commit c3b4b73

File tree

5 files changed

+23
-14
lines changed

5 files changed

+23
-14
lines changed

src/WebApp/BootstrapTheme/DynamicFieldRenderer.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ protected function renderRow($idExt, $value, $isTemplate) {
4040
else $child->setValue($value->$name);
4141
}
4242
$child->setId($id.'-'.$idExt);
43-
$child->setName($name.'[]');
43+
$child->setArray(true);
4444
$label = $child->getLabel();
4545
if ($label != NULL) {
4646
$s = $isTemplate ? 'display: none;' : '';

src/WebApp/BootstrapTheme/I18nFormElementRenderer.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ public function __construct($theme, $component) {
1212
public function render() {
1313
$languages = $this->component->getLanguages();
1414
$cId = $this->component->getId();
15-
$cName = $this->component->getBaseName();
15+
$cName = $this->component->getName();
1616
$isArray = $this->component->isArray();
1717
if (count($languages) > 1) {
1818
$tabSet = new \WebApp\Component\TabSet(NULL, $cId);

src/WebApp/BootstrapTheme/I18nTextInputRenderer.php

+1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ public function __construct($theme, $component) {
1111
protected function createFormElement($languageKey, $id, $name) {
1212
$rc = new \WebApp\Component\TextInput(NULL, $id, $this->component->getValue($languageKey));
1313
$rc->setName($name);
14+
if ($this->component->isArray()) $rc->setArray(TRUE);
1415
$rc->setEnabled($this->component->isEnabled());
1516
$rc->setRequired($this->component->isRequired());
1617
$rc->setPlaceholder($this->component->getPlaceholder());

src/WebApp/Component/FormElement.php

+17-12
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
class FormElement extends Container {
88

99
protected $label;
10+
protected $isArray;
1011
protected $help;
1112
protected $group;
1213
protected $hidden;
@@ -17,7 +18,8 @@ public function __construct($parent, $id) {
1718
$this->setId($id);
1819
$this->setName($id);
1920
}
20-
$this->hidden = FALSE;
21+
$this->hidden = FALSE;
22+
$this->isArray = FALSE;
2123
}
2224

2325
public function hide() {
@@ -50,6 +52,17 @@ public function setHelp($value) {
5052
return $this;
5153
}
5254

55+
public function isArray() {
56+
return $this->isArray;
57+
}
58+
59+
public function setArray($value) {
60+
$this->isArray = $value;
61+
foreach ($this->getChildren() AS $child) {
62+
if (method_exists($child, 'setArray')) $child->setArray($value);
63+
}
64+
}
65+
5366
public function getName() {
5467
return $this->getAttribute('name', TRUE, $this->getId());
5568
}
@@ -59,17 +72,9 @@ public function setName($name) {
5972
return $this;
6073
}
6174

62-
public function getBaseName() {
63-
$rc = $this->getName();
64-
if (mb_strpos($this->getName(), '[]') > 0) {
65-
$rc = mb_substr($rc, 0, mb_strlen($rc)-2);
66-
}
67-
return $rc;
68-
}
69-
70-
public function isArray() {
71-
return mb_strpos($this->getName(), '[]') > 0;
72-
}
75+
//public function isArray() {
76+
// return mb_strpos($this->getName(), '[]') > 0;
77+
//}
7378

7479
public function isEnabled() {
7580
return $this->getAttribute('disabled', TRUE) != 'disabled';

src/WebApp/Renderer.php

+3
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,9 @@ public function computeFinalAttributes() {
203203
$attributes = $this->getAttributes(TRUE);
204204
unset($attributes['class'], $attributes['style'], $attributes['id']);
205205
foreach ($attributes AS $name => $value) {
206+
if (($name == 'name') && (count($value) > 0) && method_exists($this->component, 'isArray') && $this->component->isArray()) {
207+
$value = array($value[0].'[]');
208+
}
206209
$rc[$name] = $value;
207210
}
208211
return $rc;

0 commit comments

Comments
 (0)