diff --git a/CHANGELOG.md b/CHANGELOG.md index 56d526bc9cb..5bf631f6847 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,6 +9,8 @@ - Fixed a bug where multi-value field inputs would be considered modified even if they weren’t, if the field type’s `isValueEmpty()` method returned `true`. ([#12858](https://github.com/craftcms/cms/issues/12858)) - Fixed a bug where asset thumbnails within secondary slideout tabs weren’t loading immediately. ([#12859](https://github.com/craftcms/cms/issues/12859)) - Fixed a bug where rebuilding the project config would lose track of image transforms’ `fill` and `upscale` settings. ([#12879](https://github.com/craftcms/cms/issues/12879)) +- Fixed a bug where blank Dropdown options weren’t showing up in the Selectize menu. ([#12880](https://github.com/craftcms/cms/issues/12880)) +- Fixed a bug where it was possible to save a Dropdown field without a value, even if the field didn’t have any blank options. ([#12880](https://github.com/craftcms/cms/issues/12880)) - Added `craft\models\ImageTransform::getConfig()`. ## 4.4.1 - 2023-03-09 diff --git a/src/fields/BaseOptionsField.php b/src/fields/BaseOptionsField.php index ed6319f3e01..d6cca3e4e19 100644 --- a/src/fields/BaseOptionsField.php +++ b/src/fields/BaseOptionsField.php @@ -399,7 +399,13 @@ public function getElementValidationRules(): array } return [ - ['in', 'range' => $range, 'allowArray' => $this->multi], + [ + 'in', + 'range' => $range, + 'allowArray' => $this->multi, + // Don't allow saving invalid blank values via Selectize + 'skipOnEmpty' => !($this instanceof Dropdown && Craft::$app->getRequest()->getIsCpRequest()), + ], ]; } diff --git a/src/fields/Dropdown.php b/src/fields/Dropdown.php index a2921ce9a96..4528fd3049d 100644 --- a/src/fields/Dropdown.php +++ b/src/fields/Dropdown.php @@ -51,14 +51,16 @@ protected function inputHtml(mixed $value, ?ElementInterface $element = null): s /** @var SingleOptionFieldData $value */ $options = $this->translatedOptions(true, $value, $element); + $hasBlankOption = ArrayHelper::contains($options, function($option) { + return isset($option['value']) && $option['value'] === ''; + }); + if (!$value->valid) { Craft::$app->getView()->setInitialDeltaValue($this->handle, $this->encodeValue($value->value)); $value = null; // Add a blank option to the beginning if one doesn't already exist - if (!ArrayHelper::contains($options, function($option) { - return isset($option['value']) && $option['value'] === ''; - })) { + if (!$hasBlankOption) { array_unshift($options, ['label' => '', 'value' => '']); } } @@ -69,6 +71,9 @@ protected function inputHtml(mixed $value, ?ElementInterface $element = null): s 'name' => $this->handle, 'value' => $this->encodeValue($value), 'options' => $options, + 'selectizeOptions' => [ + 'allowEmptyOption' => $hasBlankOption, + ], ]); }