ConfigForm: Introduce and utilize method transformEmptyValuesToNull()

This utility method serves as alternative for all previous custom solutions to prevent empty values from being persisted to INI files.

Since the IniWriter now handles NULL correctly, we're able to refrain from using array_filter for this purpose which was the actual cause for the referenced bug.

fixes #13357
This commit is contained in:
Johannes Meyer 2016-12-01 10:54:09 +01:00
parent e549c44247
commit 2fa854b0a8
7 changed files with 24 additions and 27 deletions

View File

@ -215,7 +215,7 @@ class ConfigController extends Controller
$form->setOnSuccess(function (UserBackendConfigForm $form) {
try {
$form->add(array_filter($form->getValues()));
$form->add($form::transformEmptyValuesToNull($form->getValues()));
} catch (Exception $e) {
$form->error($e->getMessage());
return false;
@ -246,12 +246,7 @@ class ConfigController extends Controller
$form->setIniConfig(Config::app('authentication'));
$form->setOnSuccess(function (UserBackendConfigForm $form) use ($backendName) {
try {
$form->edit($backendName, array_map(
function ($v) {
return $v !== '' ? $v : null;
},
$form->getValues()
));
$form->edit($backendName, $form::transformEmptyValuesToNull($form->getValues()));
} catch (Exception $e) {
$form->error($e->getMessage());
return false;

View File

@ -219,7 +219,7 @@ class NavigationController extends Controller
$form->setDefaultUrl(rawurldecode($this->params->get('url', '')));
$form->setOnSuccess(function (NavigationConfigForm $form) {
$data = array_filter($form->getValues());
$data = $form::transformEmptyValuesToNull($form->getValues());
try {
$form->add($data);
@ -266,12 +266,7 @@ class NavigationController extends Controller
$form->setUserConfig(Config::navigation($itemType, $itemOwner));
$form->setRedirectUrl($referrer === 'shared' ? 'navigation/shared' : 'navigation');
$form->setOnSuccess(function (NavigationConfigForm $form) use ($itemName) {
$data = array_map(
function ($v) {
return $v !== '' ? $v : null;
},
$form->getValues()
);
$data = $form::transformEmptyValuesToNull($form->getValues());
try {
$form->edit($itemName, $data);

View File

@ -43,7 +43,7 @@ class UsergroupbackendController extends Controller
$form->setIniConfig(Config::app('groups'));
$form->setOnSuccess(function (UserGroupBackendForm $form) {
try {
$form->add(array_filter($form->getValues()));
$form->add($form::transformEmptyValuesToNull($form->getValues()));
} catch (Exception $e) {
$form->error($e->getMessage());
return false;
@ -73,12 +73,7 @@ class UsergroupbackendController extends Controller
$form->setIniConfig(Config::app('groups'));
$form->setOnSuccess(function (UserGroupBackendForm $form) use ($backendName) {
try {
$form->edit($backendName, array_map(
function ($v) {
return $v !== '' ? $v : null;
},
$form->getValues()
));
$form->edit($backendName, $form::transformEmptyValuesToNull($form->getValues()));
} catch (Exception $e) {
$form->error($e->getMessage());
return false;

View File

@ -181,10 +181,10 @@ class ResourceConfigForm extends ConfigForm
return false;
}
}
$this->add(array_filter($this->getValues()));
$this->add(static::transformEmptyValuesToNull($this->getValues()));
$message = $this->translate('Resource "%s" has been successfully created');
} else { // edit existing resource
$this->edit($resource, array_filter($this->getValues()));
$this->edit($resource, static::transformEmptyValuesToNull($this->getValues()));
$message = $this->translate('Resource "%s" has been successfully changed');
}
} catch (InvalidArgumentException $e) {

View File

@ -58,7 +58,7 @@ class ConfigForm extends Form
{
$sections = array();
foreach ($this->getValues() as $sectionAndPropertyName => $value) {
if ($value === '') {
if (empty($value)) {
$value = null; // Causes the config writer to unset it
}
@ -127,4 +127,16 @@ class ConfigForm extends Form
{
$config->saveIni();
}
/**
* Transform all empty values of the given array to null
*
* @param array $values
*
* @return array
*/
public static function transformEmptyValuesToNull(array $values)
{
return array_map(function ($v) { return empty($v) ? null : $v; }, $values);
}
}

View File

@ -289,7 +289,7 @@ class RoleForm extends ConfigForm
*/
public function getValues($suppressArrayNotation = false)
{
$values = array_filter(parent::getValues($suppressArrayNotation));
$values = static::transformEmptyValuesToNull(parent::getValues($suppressArrayNotation));
if (isset($values['permissions'])) {
$values['permissions'] = implode(', ', $values['permissions']);
}

View File

@ -106,7 +106,7 @@ class ConfigController extends Controller
$form->setOnSuccess(function (BackendConfigForm $form) {
try {
$form->add(array_filter($form->getValues()));
$form->add($form::transformEmptyValuesToNull($form->getValues()));
} catch (Exception $e) {
$form->error($e->getMessage());
return false;
@ -258,7 +258,7 @@ class ConfigController extends Controller
);
$form->setOnSuccess(function (TransportConfigForm $form) {
try {
$form->add(array_filter($form->getValues()));
$form->add($form::transformEmptyValuesToNull($form->getValues()));
} catch (Exception $e) {
$form->error($e->getMessage());
return false;