Skip to content

Commit

Permalink
Fixed Dropdown/Selectize bugs
Browse files Browse the repository at this point in the history
Fixes #12880
  • Loading branch information
brandonkelly committed Mar 13, 2023
1 parent 25b08ee commit de5ed75
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 4 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 7 additions & 1 deletion src/fields/BaseOptionsField.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()),
],
];
}

Expand Down
11 changes: 8 additions & 3 deletions src/fields/Dropdown.php
Original file line number Diff line number Diff line change
Expand Up @@ -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' => '']);
}
}
Expand All @@ -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,
],
]);
}

Expand Down

0 comments on commit de5ed75

Please sign in to comment.