icingaweb2-module-director/library/Director/Web/Form/Element/ExtensibleSet.php
Ravi Srinivasa add4301750
Some checks are pending
L10n Update / update (push) Waiting to run
PHP Tests / Static analysis for php 7.2 on ubuntu-latest (push) Waiting to run
PHP Tests / Static analysis for php 7.3 on ubuntu-latest (push) Waiting to run
PHP Tests / Static analysis for php 7.4 on ubuntu-latest (push) Waiting to run
PHP Tests / Static analysis for php 8.0 on ubuntu-latest (push) Waiting to run
PHP Tests / Static analysis for php 8.1 on ubuntu-latest (push) Waiting to run
PHP Tests / Static analysis for php 8.2 on ubuntu-latest (push) Waiting to run
PHP Tests / Static analysis for php 8.3 on ubuntu-latest (push) Waiting to run
PHP Tests / Unit tests with php 8.2 on ubuntu-latest (push) Waiting to run
PHP Tests / Unit tests with php 8.3 on ubuntu-latest (push) Waiting to run
Revert the changes made for shardj/zf1-future compatibility in (#3035)
The patch that was dropped in icinga-php-thidparty version 0.15.0, will be applied in the Icinga/zf1 fork.
And with the icinga-php-thirdparty version 0.15.1, this changes are obsolete and also will not break compatibility
with earlier versions of icinga-php-thirdparty.
2026-03-31 17:30:28 +02:00

89 lines
2 KiB
PHP

<?php
namespace Icinga\Module\Director\Web\Form\Element;
use InvalidArgumentException;
/**
* Input control for extensible sets
*/
class ExtensibleSet extends FormElement
{
/**
* Default form view helper to use for rendering
* @var string
*/
public $helper = 'formIplExtensibleSet';
// private $multiOptions;
public function getValue()
{
$value = parent::getValue();
if (is_string($value) || is_numeric($value)) {
$value = [$value];
} elseif ($value === null) {
return $value;
}
if (! is_array($value)) {
throw new InvalidArgumentException(sprintf(
'ExtensibleSet expects to work with Arrays, got %s',
var_export($value, true)
));
}
$value = array_filter($value, 'strlen');
if (empty($value)) {
return null;
}
return $value;
}
/**
* We do not want one message per entry
*
* @codingStandardsIgnoreStart
*/
protected function _getErrorMessages()
{
return $this->_errorMessages;
// @codingStandardsIgnoreEnd
}
/**
* @codingStandardsIgnoreStart
*/
protected function _filterValue(&$value, &$key)
{
// @codingStandardsIgnoreEnd
if (is_array($value)) {
$value = array_filter($value, 'strlen');
} elseif (is_string($value) && !strlen($value)) {
$value = null;
}
parent::_filterValue($value, $key);
}
public function isValid($value, $context = null)
{
if ($value === null) {
$value = [];
}
$value = array_filter($value, 'strlen');
$this->setValue($value);
if ($this->isRequired() && empty($value)) {
// TODO: translate
$this->addError('You are required to choose at least one element');
return false;
}
if ($this->hasErrors()) {
return false;
}
return parent::isValid($value, $context);
}
}