Skip to content

Commit

Permalink
ChannelForm: Render default values as element values
Browse files Browse the repository at this point in the history
Instead of rendering default values as placeholders, render them as form element values.
  • Loading branch information
raviks789 committed Apr 11, 2024
1 parent 110764e commit 6fc00d4
Showing 1 changed file with 22 additions and 7 deletions.
29 changes: 22 additions & 7 deletions application/forms/ChannelForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ class ChannelForm extends CompatForm
/** @var ?int Channel ID */
private $channelId;

/** @var array<string, mixed> */
private $defaultChannelOptions = [];

public function __construct(Connection $db, ?int $channelId = null)
{
$this->db = $db;
Expand Down Expand Up @@ -161,7 +164,14 @@ protected function onSuccess()
}

$channel = $this->getValues();
$channel['config'] = json_encode($channel['config']);
$config = $channel['config'];
foreach ($this->defaultChannelOptions as $key => $value) {
if (array_key_exists($key, $config) && $config[$key] === $value) {
$config[$key] = null;
}
}

$channel['config'] = json_encode($config);
if ($this->channelId === null) {
$this->db->insert('channel', $channel);
} else {
Expand All @@ -188,17 +198,25 @@ protected function createConfigElements(string $type, string $config): void
$this->addElement($configFieldset);

foreach ($elementsConfig as $elementConfig) {
$elemOptions = $this->getElementOptions($elementConfig);
/** @var BaseFormElement $elem */
$elem = $this->createElement(
$this->getElementType($elementConfig),
$elementConfig->name,
$this->getElementOptions($elementConfig)
$elemOptions
);

if ($type === "email" && $elem->getName() === "sender_mail") {
$elem->getValidators()->add(new EmailAddressValidator());
}

if (
isset($elementConfig->default)
&& $configFieldset->getPopulatedValue($elementConfig->name) === null
) {
$configFieldset->clearPopulatedValue($elementConfig->name);
}

$configFieldset->addElement($elem);
}
}
Expand Down Expand Up @@ -270,11 +288,8 @@ protected function getElementOptions(stdClass $elementConfig): array
}

if (isset($elementConfig->default)) {
if ($isSelectElement || $elementConfig->type === 'bool') {
$options['value'] = $elementConfig->default;
} else {
$options['placeholder'] = $elementConfig->default;
}
$this->defaultChannelOptions[$elementConfig->name] = $elementConfig->default;

Check failure on line 291 in application/forms/ChannelForm.php

View workflow job for this annotation

GitHub Actions / phpstan / Static analysis with phpstan and php 7.2 on ubuntu-latest

Property Icinga\Module\Notifications\Forms\ChannelForm::$defaultChannelOptions (array<string, mixed>) does not accept array<int|string, mixed>.

Check failure on line 291 in application/forms/ChannelForm.php

View workflow job for this annotation

GitHub Actions / phpstan / Static analysis with phpstan and php 7.2 on ubuntu-latest

Property Icinga\Module\Notifications\Forms\ChannelForm::$defaultChannelOptions (array<string, mixed>) does not accept array<int|string, mixed>.

Check failure on line 291 in application/forms/ChannelForm.php

View workflow job for this annotation

GitHub Actions / phpstan / Static analysis with phpstan and php 7.3 on ubuntu-latest

Property Icinga\Module\Notifications\Forms\ChannelForm::$defaultChannelOptions (array<string, mixed>) does not accept array<int|string, mixed>.

Check failure on line 291 in application/forms/ChannelForm.php

View workflow job for this annotation

GitHub Actions / phpstan / Static analysis with phpstan and php 7.4 on ubuntu-latest

Property Icinga\Module\Notifications\Forms\ChannelForm::$defaultChannelOptions (array<string, mixed>) does not accept array<int|string, mixed>.

Check failure on line 291 in application/forms/ChannelForm.php

View workflow job for this annotation

GitHub Actions / phpstan / Static analysis with phpstan and php 8.0 on ubuntu-latest

Property Icinga\Module\Notifications\Forms\ChannelForm::$defaultChannelOptions (array<string, mixed>) does not accept array<int|string, mixed>.

Check failure on line 291 in application/forms/ChannelForm.php

View workflow job for this annotation

GitHub Actions / phpstan / Static analysis with phpstan and php 8.1 on ubuntu-latest

Property Icinga\Module\Notifications\Forms\ChannelForm::$defaultChannelOptions (array<string, mixed>) does not accept array<int|string, mixed>.

Check failure on line 291 in application/forms/ChannelForm.php

View workflow job for this annotation

GitHub Actions / phpstan / Static analysis with phpstan and php 8.2 on ubuntu-latest

Property Icinga\Module\Notifications\Forms\ChannelForm::$defaultChannelOptions (array<string, mixed>) does not accept array<int|string, mixed>.

Check failure on line 291 in application/forms/ChannelForm.php

View workflow job for this annotation

GitHub Actions / phpstan / Static analysis with phpstan and php 8.3 on ubuntu-latest

Property Icinga\Module\Notifications\Forms\ChannelForm::$defaultChannelOptions (array<string, mixed>) does not accept array<int|string, mixed>.
$options['value'] = $elementConfig->default;
}

if ($elementConfig->type === "number") {
Expand Down

0 comments on commit 6fc00d4

Please sign in to comment.