YesNo: new boolean-like form fields

fixes #12927
This commit is contained in:
Thomas Gelf 2016-10-14 13:39:43 +00:00
parent 98708e6496
commit 6f46e6b496
4 changed files with 41 additions and 34 deletions

View File

@ -124,14 +124,12 @@ class SyncPropertyForm extends DirectorObjectForm
}
*/
// TODO: we need modifier
$this->addElement('select', 'use_filter', array(
$this->addElement('YesNo', 'use_filter', array(
'label' => $this->translate('Set based on filter'),
'ignore' => true,
'class' => 'autosubmit',
'required' => true,
'multiOptions' => $this->enumBoolean()
));
if ($this->hasBeenSent()) {

View File

@ -229,12 +229,6 @@ abstract class DirectorObjectForm extends QuickForm
unset($props['vars']);
}
foreach ($props as $k => $v) {
if (is_bool($v)) {
$props[$k] = $v ? 'y' : 'n';
}
}
$this->setDefaults($props);
if (! $object instanceof IcingaObject) {
@ -424,25 +418,12 @@ abstract class DirectorObjectForm extends QuickForm
protected function addBoolean($key, $options, $default = null)
{
$map = array(
false => 'n',
true => 'y',
'n' => 'n',
'y' => 'y',
);
if ($default !== null) {
$options['multiOptions'] = $this->enumBoolean();
if ($default === null) {
return $this->addElement('OptionalYesNo', $key, $options);
} else {
$options['multiOptions'] = $this->optionalEnum($this->enumBoolean());
$this->addElement('YesNo', $key, $options);
return $this->getElement($key)->setValue($default);
}
$res = $this->addElement('select', $key, $options);
if ($default !== null) {
$this->getElement($key)->setValue($map[$default]);
}
return $res;
}
protected function optionalBoolean($key, $label, $description)
@ -453,14 +434,6 @@ abstract class DirectorObjectForm extends QuickForm
));
}
protected function enumBoolean()
{
return array(
'y' => $this->translate('Yes'),
'n' => $this->translate('No'),
);
}
public function hasElement($name)
{
return $this->getElement($name) !== null;

View File

@ -0,0 +1,22 @@
<?php
namespace Icinga\Module\Director\Web\Form\Element;
/**
* Input control for booleans, gives y/n
*/
class OptionalYesNo extends Boolean
{
public function getValue()
{
$value = $this->getUnfilteredValue();
if ($value === 'y' || $value === true) {
return 'y';
} elseif ($value === 'n' || $value === false) {
return 'n';
}
return null;
}
}

View File

@ -0,0 +1,14 @@
<?php
namespace Icinga\Module\Director\Web\Form\Element;
/**
* Input control for booleans, gives y/n
*/
class YesNo extends OptionalYesNo
{
public $options = array(
'y' => 'Yes',
'n' => 'No',
);
}