Commit 1bc9bb8c authored by Carsten  Rose's avatar Carsten Rose
Browse files

AbstractBuildForm.php: BuildCheckBoxMulti() fixed problem with preselected value

Save: handle CheckBox Multi values correctly
parent f3bf8fd5
......@@ -570,7 +570,7 @@ abstract class AbstractBuildForm {
/**
* Builds HTML 'checkbox' element.
*
* Checkboxes will only be submitted, if they are checked. Therefore, a hidden element with the unchecked value will be transfered first.
* Checkboxes will only be submitted, if they are checked. Therefore, a hidden element with the unchecked value will be transferred first.
*
* Format: <input type="hidden" name="$htmlFormElementId" value="$valueUnChecked">
* <input name="$htmlFormElementId" type="checkbox" [autofocus="autofocus"]
......@@ -598,7 +598,7 @@ abstract class AbstractBuildForm {
if ($formElement['checkBoxMode'] === 'multi') {
$htmlFormElementId .= '[]';
} else {
// Fill meaningfull defaults to parameter:checked|unchecked
// Fill meaningfull defaults to parameter: checked|unchecked (CHECKBOX_VALUE_CHECKED|CHECKBOX_VALUE_UNCHECKED)
$this->prepareCheckboxCheckedUncheckedValue($itemKey, $formElement);
}
......@@ -737,27 +737,27 @@ abstract class AbstractBuildForm {
*/
private function prepareCheckboxCheckedUncheckedValue(array $itemKey, array &$formElement) {
if (!isset($formElement['checked'])) {
if (!isset($formElement[CHECKBOX_VALUE_CHECKED])) {
if (isset($itemKey[0])) {
// First element in $itemKey list
$formElement['checked'] = $itemKey[0];
$formElement[CHECKBOX_VALUE_CHECKED] = $itemKey[0];
} else {
// Take column default value
$formElement['checked'] = $this->store->getVar($formElement['name'], STORE_TABLE_DEFAULT);
$formElement[CHECKBOX_VALUE_CHECKED] = $this->store->getVar($formElement['name'], STORE_TABLE_DEFAULT);
}
}
// unchecked
if (!isset($formElement['unchecked'])) {
if (!isset($formElement[CHECKBOX_VALUE_UNCHECKED])) {
if (isset($itemKey[1])) {
$formElement['unchecked'] = ($itemKey[0] === $formElement['checked']) ? $itemKey[1] : $itemKey[0];
$formElement[CHECKBOX_VALUE_UNCHECKED] = ($itemKey[0] === $formElement['checked']) ? $itemKey[1] : $itemKey[0];
} else {
$formElement['unchecked'] = '';
$formElement[CHECKBOX_VALUE_UNCHECKED] = '';
}
}
if ($formElement['checked'] === $formElement['unchecked']) {
throw new UserException('FormElement: type=checkbox - checked and unchecked can\'t be the same: ' . $formElement['checked'], ERROR_CHECKBOX_EQUAL);
if ($formElement[CHECKBOX_VALUE_CHECKED] === $formElement[CHECKBOX_VALUE_UNCHECKED]) {
throw new UserException('FormElement: type=checkbox - checked and unchecked can\'t be the same: ' . $formElement[CHECKBOX_VALUE_CHECKED], ERROR_CHECKBOX_EQUAL);
}
}
......@@ -815,28 +815,30 @@ abstract class AbstractBuildForm {
*/
public function buildCheckboxMulti(array $formElement, $htmlFormElementId, $attributeBase, $value, array $itemKey, array $itemValue) {
// Defines which of the checkboxes will be checked.
$values = explode($value, ',');
$values = explode(',', $value);
$attributeBase .= $this->getAttribute('name', $htmlFormElementId);
$html = $this->buildNativeHidden($htmlFormElementId, $value);
$html = $this->buildNativeHidden($htmlFormElementId, '');
$flagFirst = true;
$ii = 0;
$jj = 0;
foreach ($itemKey as $item) {
$ii++;
$jj++;
for ($ii = 0; $ii < count($itemKey); $ii++) {
$attribute = $attributeBase;
// Do this only the first round.
if ($flagFirst) {
$flagFirst = false;
if (isset($formElement['autofocus']))
$attribute .= $this->getAttribute('autofocus', $formElement['autofocus']);
}
$attribute .= $this->getAttribute('value', $item);
if ($item === $values[$jj]) {
$attribute .= $this->getAttribute('value', $itemKey[$ii]);
// Check if the given key is found in field.
if (false !== array_search($itemKey[$ii], $values)) {
$attribute .= $this->getAttribute('checked', 'checked');
}
$html .= '<input ' . $attribute . '>';
$html .= $itemValue[$ii];
if ($ii === $formElement['maxLength']) {
......@@ -844,6 +846,7 @@ abstract class AbstractBuildForm {
$html .= '<br>';
}
}
return $html;
}
......
......@@ -79,11 +79,12 @@ class Save {
$tableColumns = array_keys($this->store->getStore(STORE_TABLE_COLUMN_TYPES));
$clientValues = $this->store->getStore(STORE_CLIENT);
// Retrieve SIP vars, e.g. for HIDDEN elements.
$sipValues = $this->store->getStore(STORE_SIP);
// Iterate over all table.columns. Built an assoc array $newValues.
foreach ($tableColumns AS $column) {
// Never save a predefined 'id'.
// Never save a predefined 'id': autoincrement values will be given by database..
if ($column === 'id')
continue;
......@@ -92,7 +93,7 @@ class Save {
if ($formElement === false)
continue;
// Log / Debug
// Preparation for Log, Debug
$this->store->setVar(SYSTEM_FORM_ELEMENT, $formElement['name'] . ' / ' . $formElement['id'], STORE_SYSTEM);
if ($formElement['type'] == 'hidden') {
......@@ -100,13 +101,26 @@ class Save {
if (!isset($sipValues[$column])) {
throw new CodeException("Missing the hidden field '$column' in SIP.", ERROR_MISSING_HIDDEN_FIELD_IN_SIP);
}
$newValues[$column] = $sipValues[$column];
continue;
}
// construct the field name used in the form
$clientFieldName = HelperFormElement::buildFormElementId($column, $recordId);
if (isset($clientValues[$clientFieldName])) {
// SELECT with multiple values, or Multi CHECKBOX are delivered as array: implode them
if (is_array($clientValues[$clientFieldName])) {
// E.g. Checkboxes needs a 'HIDDEN' HTML input to detect 'unset' of values. These 'HIDDEN' element
// needs to be removed, if there is at least one checkbox is checked (=submitted)
if (count($clientValues[$clientFieldName]) > 1)
array_shift($clientValues[$clientFieldName]);
$clientValues[$clientFieldName] = implode(',', $clientValues[$clientFieldName]);
}
$newValues[$column] = $this->validateValue($formElement, $clientValues[$clientFieldName]);
}
......@@ -239,6 +253,7 @@ class Save {
$sql = 'UPDATE `' . $tableName . '` SET ';
foreach ($values as $column => $value) {
$sql .= '`' . $column . '` = ?, ';
}
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment